Home > Web Front-end > JS Tutorial > body text

jquery sets the top value of an element relative to another element (example code)_jquery

WBOY
Release: 2016-05-16 17:17:14
Original
1240 people have browsed it

sfdsfsddfsdf

                                                                                                                                          

Set the button below the input

$("#button").css("{top":$("#input").offset().top-$("#span2").offset().top $("#input ").height,position:"absolute"});

In this way, wherever the opinion input button is located, the button will be below the input. The same can be applied to the calendar widget below the input text box

1. In jquery, offset().top is relative to the body. In addition, when setting the top value, you need to find the element with a relative value that is closest to the element

You can write it like this in js:


// Get the absolute position of the HTML control
Calendar.prototype.getAbsPoint = function (e){
var x = e.offsetLeft;
var y = e.offsetTop;
while(e = e.offsetParent) {
x = e.offsetLeft;
y = e.offsetTop;
}
return {"x": x, "y": y};
}

var xy = this.getAbsPoint(popControl); this.panel.style.left = xy.x "px";


this.panel.style.top = (xy.y dateObj.offsetHeight) "px";


As shown in the picture:


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template