JQuery.offset y value

I am new to jQuery. Can someone please answer this?

I know that I will set layer1 to layer2 with the following line of code.

$("#layer1").offset($("#layer2").offset());

How can I just set the y value? I am not sure about that.

thank

+5
source share
5 answers

The jQuery documentation for .offest()reads:

.offset () returns an object containing properties on the top and left.

Knowing this, you can do the following:

var offset = $("#layer2").offset();
$("#layer1").css({
    'top' : offset.top,
    'left': offset.left
});  

Or you can install them individually, according to your requirement.

$("#layer1").css('top', offset.top);  // or...
$("#layer1").css('left', offset.left);

Finally, since you only need one value (above), the offset is redundant; it is more expensive than you need. Use the following optimized snippet instead.

var top = $('#layer2').css('top');
$('#layer1').css('top', top);
+7

/ css top

$("#layer1").css("top",$("#layer2").css("top"));
0
$("#layer1").css({
  'top': $("#layer2").offset().top
});

EDIT .top / .left seems to be setters.
EDITv2 offset can be set, but must be done using the .offset () method.

0
source

According to http://api.jquery.com/offset/

$("p:last").offset({ top: 10, left: 30 });

. Thus, you should be able to simply delete what you want, donโ€™t want.

0
source

Use offset.top and offset.left - this is x and y .. offset () by itself returns both x and y (or top and left)

0
source

All Articles