my extension is a frame that share the screen with any webpages. I have a button that calls this function(dot.js), but its printing over my frame too. How can i set the area allowed to draw the dots?
dot.js
function printMousePos(event, autor) {
var div = document.createElement("div");
div.style.height = "10px";
div.style.width = "10px";
div.style.backgroundColor = "black";
div.style.position = "fixed";
div.style.left = event.clientX+"px";
div.style.top = event.clientY+"px";
div.style.borderRadius = "50px";
console.log(
"clientX: " + event.clientX +
" - clientY: " + event.clientY);
document.body.appendChild(div);
}
document.addEventListener("mousedown", function(evt){
printMousePos(evt, "autor1");
})
;
Thanks
Related
Currently I am using this code:
var mousePosition;
var offset = [0,0];
var div;
var isDown = false;
div = document.createElement("div");
div.style.position = "absolute";
div.style.left = "0px";
div.style.top = "0px";
div.style.width = "237px";
div.style.height = "50px";
div.style.background = "red";
div.style.padding = "15px 0px 0px 0px";
div.style.color = "blue";
div.id = "clock";
document.body.appendChild(div);
div.addEventListener('mousedown', function(e) {
isDown = true;
offset = [
div.offsetLeft - e.clientX,
div.offsetTop - e.clientY
];
}, true);
document.addEventListener('mouseup', function() {
isDown = false;
}, true);
document.addEventListener('mousemove', function(event) {
event.preventDefault();
if (isDown) {
mousePosition = {
x : event.clientX,
y : event.clientY
};
div.style.left = (mousePosition.x + offset[0]) + 'px';
div.style.top = (mousePosition.y + offset[1]) + 'px';
}
}, true);
The code allows me to move a div using my mouse. I'm not very experienced with JavaScript.
But I was hoping there is an easy method to cache the end location of the Div. So that when I close and open the HTML file, the Div will start in its last cached location.
Thanks.
You can use localStorage to cache the location information into your browser.
window.localStorage.setItem('divPosition', JSON.stringify({
left: div.style.left,
top: div.style.top
}));
console.log(JSON.parse(window.localStorage.getItem('divPosition')));
One caveat is that localStorage doesn't reliably accept anything but string values so adding more robust information requires stringifying and parsing the data.
I would like to draw a 2 x 2 grid of blue rectangles on an HTML page, but my code does not draw anything. I create a fragment toAdd that I want to add later, and add divs to toAdd. I'm not quite sure where I went wrong, and when I tried to add a console.log() I could confirm that addSquares is being called. Do I need to add anything to the HTML file, or is there a mistake in this code?
I also noticed that this code produces five divs: b0, b1, b2, b3, b1 (returns error) and I'm not sure what is wrong with my for loop.
dim = 2;
width = 50;
height = 50;
// add the squares
function addSquares()
{
var toAdd = document.createDocumentFragment();
for(var i = 0; i < dim; i++)
{
for(var j = 0; j < dim; j++)
{
var label = j + i * dim;
var name = "b" + label;
console.log(name)
var div = document.createElement(name);
div.style.width = width + "px";
div.style.height = height + "px";
div.style.left = width * j + "px";
div.style.top = height * i + "px";
div.style.position = "absolute";
div.style.color = "blue";
toAdd.appendChild(div);
}
}
document.appendChild(toAdd);
}
So it appears that there's two issues. The first being that you need to change:
document.appendChild(toAdd);
to
document.body.appendChild(toAdd);
Your second issue is that your using the wrong CSS styling declaration. Your divs are in the DOM but don't have a backgroundColor assigned to them, which in turn gives the illusion that they're not there. So instead of color use backgroundColor.
dim = 2;
width = 50;
height = 50;
// add the squares
function addSquares()
{
var toAdd = document.createDocumentFragment();
for(var i = 0; i < dim; i++)
{
for(var j = 0; j < dim; j++)
{
var label = j + i * dim;
var name = "b" + label;
console.log(name)
var div = document.createElement(name);
div.style.width = width + "px";
div.style.height = height + "px";
div.style.left = width * j + "px";
div.style.top = height * i + "px";
div.style.position = "absolute";
div.style.backgroundColor = "blue";
toAdd.appendChild(div);
}
}
document.body.appendChild(toAdd);
}
addSquares();
I just wanted to point out a little trick to you...
You have this:
div.style.width = width + "px";
div.style.height = height + "px";
div.style.left = width * j + "px";
div.style.top = height * i + "px";
div.style.position = "absolute";
div.style.backgroundColor = "blue";
I believe can be written like this ES6:
div.style.cssText = `width: ${width + "px"};
height: ${height + "px"};
left: ${width * j + "px"};
top: ${height * i + "px"};
position: absolute;
backgroundColor: blue;`
As far as I know .cssText = will replace the existing inline...
To append the existing inline use .cssText +=
I'm sure this will help you in the long run.
Why is element A invisible? It seems that appendChild() function is ignored. I can only see the B element. (element A is visible if I push it inside the controls) :(
var a = document.createElement('A');
a.style.width = '50px';
a.style.height = '50px';
a.style.cursor = 'pointer';
a.style.backgroundColor = 'rgba(10,20,30,1.0)';
var b = document.createElement('B');
b.style.width = '200px';
b.style.height = '200px';
b.style.backgroundColor = 'rgba(230,230,230,0.6)';
b.appendChild(a);
map.controls[google.maps.ControlPosition.LEFT].push(b);
Your problem is that you have to display both the elements on a block level
var a = document.createElement('A');
a.style.width = '50px';
a.style.height = '50px';
a.style.cursor = 'pointer';
a.style.backgroundColor = 'rgba(10,20,30,1.0)';
a.style.display = 'block'; //style block level
var b = document.createElement('B');
b.style.width = '200px';
b.style.height = '200px';
b.style.backgroundColor = 'rgba(230,230,230,0.6)';
b.style.border = '1px solid black';
b.style.display = 'block'; //style block level
document.body.appendChild(b);
b.appendChild(a);
https://jsfiddle.net/9rnzbuqh/
<a> is an inline element by default, and you can't set width or height of an inline element. You have to change its display to block:
var a = document.createElement('A');
a.style.width = '50px';
a.style.height = '50px';
a.style.cursor = 'pointer';
a.style.backgroundColor = 'rgba(10,20,30,1.0)';
a.style.display = 'block';
var b = document.createElement('B');
b.style.width = '200px';
b.style.height = '200px';
b.style.backgroundColor = 'rgba(230,230,230,0.6)';
b.style.display = 'block';
b.appendChild(a);
map.controls[google.maps.ControlPosition.LEFT].push(b);
I figured out what I was doing. It worked without setting the display style but I had no idea why.
https://jsfiddle.net/9rnzbuqh/78/
var c = document.createElement('div');
c.style.height = '263px';
c.style.width = '350px';
c.style.marginLeft = '0px';
c.style.marginTop = '0px';
c.style.backgroundImage = "url(https://upload.wikimedia.org/wikipedia/en/5/5f/TomandJerryTitleCardc.jpg)";
var d = document.createElement('div');
d.style.background = 'rgba(230,230,230,0.6)';
d.style.paddingBottom = '70px';
d.style.paddingLeft = '40px';
d.style.paddingRight = '40px';
d.appendChild(c);
document.body.appendChild(d);
and
div {
width:400px;
}
Hi i would like to create new div onmousedown and i would like to resize it on mouse move. But i can't seem to get the mistake i've made.
var x1;
var y1;
var pressed = false;
document.getElementById("primary").onmousedown = function() {
pressed = true;
var ok = true;
if (ok === true) {
div = document.createElement('div');
div.style.backgroundColor = "black";
div.style.position = "absolute";
x1 = Math.round(event.clientX);
y1 = Math.round(event.clientY);
div.style.left = x1 + "px";
div.style.top = y1 + "px";
div.setAttribute("id", "uniqueIdentifier");
document.getElementsByTagName('body')[0].appendChild(div);
}
};
document.getElementById("primary").onmousemove = function() {
if (pressed) {
var div = get.getElementById("uniqueIdentifier");
var x2 = Math.round(event.clientX) + x1;
var y2 = Math.round(event.clientY) + y1;
div.style.width = x2 + "px";
div.style.height = y2 + "px";
}
}
document.getElementById("primary").onmouseup = function() {
pressed = false;
}
#primary {
height: 200px;
width: 200px;
background-color: yellow;
}
<div id="primary"></div>
You had a typo. get.getElementById should be document.getElementById. When I fix that, the code works.
var x1;
var y1;
var pressed = false;
document.getElementById("primary").onmousedown = function() {
pressed = true;
var ok = true;
if (ok === true) {
div = document.createElement('div');
div.style.backgroundColor = "black";
div.style.position = "absolute";
x1 = Math.round(event.clientX);
y1 = Math.round(event.clientY);
div.style.left = x1 + "px";
div.style.top = y1 + "px";
div.setAttribute("id", "uniqueIdentifier");
document.getElementsByTagName('body')[0].appendChild(div);
}
};
document.getElementById("primary").onmousemove = function() {
if (pressed) {
var div = document.getElementById("uniqueIdentifier");
var x2 = Math.round(event.clientX) + x1;
var y2 = Math.round(event.clientY) + y1;
div.style.width = x2 + "px";
div.style.height = y2 + "px";
}
}
document.getElementById("primary").onmouseup = function() {
pressed = false;
}
#primary {
height: 200px;
width: 200px;
background-color: yellow;
}
<div id="primary"></div>
I am trying to get this code to work in a way that where I click on item one it will do one function, and when I click on item two it will do a different function. Right now they all go to the same thing and I can't seem to pull it apart.
var list = [];
list[0] = ["zero"];
list[1] = ["one"];
list[2] = ["two"];
list[3] = ["three"];
function Make(){
for ( var i = 0; i < 4 ; i++ ) {
var div = document.createElement("div");
div.style.width = "10px";
div.style.height = "10px";
div.style.background = "white";
div.style.color = "black";
div.style.top = "0px";
div.style.left = "0px";
div.style.margin = "10px 10px auto";
div.style.cursor = "pointer";
div.innerHTML = list[i];
!function(){
var index = 0;
div.onclick = function () { alert("this works"); };
}();
document.body.appendChild(div);
}
}
Did you mean something like this?
<script type="text/javascript">
var list = [];
list[0] = ["zero"];
list[1] = ["one"];
list[2] = ["two"];
list[3] = ["three"];
function Make(){
for ( var i = 0; i < 4 ; i++ ) {
var div = document.createElement("div");
div.style.width = "10px";
div.style.height = "10px";
div.style.background = "white";
div.style.color = "black";
div.style.top = "0px";
div.style.left = "0px";
div.style.margin = "10px 10px auto";
div.style.cursor = "pointer";
div.innerHTML = list[i];
!function(){
var index = 0;
div.onclick = function () { doSomething(this); };
}();
document.body.appendChild(div);
}
}
function doSomething(element){
var value = element.innerHTML;
alert('clicked : '+ value);
switch(value){
case "zero":
//Your code here
break;
case "one":
//Your code here
break;
}
}
Make();
</script>