4
For example, I have a div
within a variable obj
obj = document.getElementById("objeto");
..I want a function to be applied to it that way
window.onload = function(){
window.onclick = function (){
obj = document.getElemntById("obj");
obj.ver();
}
function ver(){
this.style.backgroundColor = "#333333";
}
}
How could I make this application work?
My complete code, abixo:
<script>
HTMLElement.prototype.mover = function(){
movendo = false;
valor = 0;
calc = 0;
resto = 0;
this.onmousedown = function(e){
movendo = true;
valor = e.pageX + document.body.scrollLeft;
calc = valor - this.offsetLeft;
resto = this.offsetWidth - calc;
valor2 = e.pageY + document.body.scrollTop;
calc2 = valor2 - this.offsetTop;
resto2 = this.offsetHeight - calc2;
this.style.cursor = "move";
};
window.onmouseup = function(){
movendo = false;
this.style.cursor = "pointer";
};
window.onmousemove = function(e){
if( movendo == true ) {
this.style.left = (e.pageX + document.body.scrollLeft) - this.offsetWidth + resto + "px";
this.style.top = (e.pageY + document.body.scrollTop) - this.offsetHeight + resto2 + "px";
}
};
}
window.onload = function(){
obj = document.getElementById("obj");
obj.mover();
}
</script>
This site explains how to do a lot of things without using jQuery. You might find it useful: http://youmightnotneedjquery.com/
– user7261
Andrey, thank you, yes will help me!
– Samir Braga
@Samirbraga, I left a comment below, here’s a jsFiddle version too: http://jsfiddle.net/M238g/
– Sergio