Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set offset() 100px in this script?

Tags:

jquery

css

How can I set the offset() function to do what margin-top: 100px; would do?

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>

    <script type="text/javascript">
    $(function() {
        $(document).scrollTop( $("#position_page").offset().top );  
    });
    </script>

    <div id="aboutUs">
      About us content...
    </div><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <div id="position_page">
      Header content...
    </div>
like image 969
user3886651 Avatar asked Jul 29 '14 13:07

user3886651


People also ask

How do you set offset value?

jQuery offset() MethodThe offset() method set or returns the offset coordinates for the selected elements, relative to the document. When used to return the offset: This method returns the offset coordinates of the FIRST matched element. It returns an object with 2 properties; the top and left positions in pixels.

What is offset () Top?

The offsetTop property returns the top position (in pixels) relative to the parent. The returned value includes: the top position, and margin of the element. the top padding, scrollbar and border of the parent.

What is the difference between position and offset in jQuery?

Difference between offset() and position() Method:The jQuery UI offset() is relative to the document. The jQuery UI position() is relative to the parent element. When you want to position a new element on top of another one which is already existing, its better to use the jQuery offset() method.


2 Answers

You can just add on the offset to the end of the scrollTop like this:

$(document).scrollTop( $("#position_page").offset().top + 100);  

Here is a working example: http://jsfiddle.net/mtruty/rks3H/

like image 105
mdt0093 Avatar answered Sep 26 '22 14:09

mdt0093


simply add 100 to the scroll position

<script type="text/javascript">
$(function() {
    $(document).scrollTop( $("#position_page").offset().top + 100);  
});
</script>

<div id="aboutUs">
  About us content...
</div><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="position_page">
  Header content...
</div>
like image 21
springbo Avatar answered Sep 23 '22 14:09

springbo