SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help dynamically calculating "bottom" relative to another div - using jquery?

    hi all,

    i'm a novice at scripting etc, but have been tying the below code:


    Code:
    $(function() {
        $.fn.scrollBottom = function() {
            return $(document).height() - this.scrollTop() - this.height();
        };
    
        var $el = $('#wrap');
        var $window = $(window);
    
        $window.bind("scroll resize", function() {
            var gap = $window.height() - $el.height() - 10;
            var visibleFoot = 1233 - $window.scrollBottom();
            var scrollTop = $window.scrollTop()
    
            if(scrollTop < 100 + 10){
                $el.css({
                    top: (100 - scrollTop) + "px",
                    bottom: "auto"
                });
            }else if (visibleFoot > gap) {
                $el.css({
                    top: "auto",
                    bottom: visibleFoot + "px"
                });
            } else {
                $el.css({
                    top: 0,
                    bottom: "auto"
                });
            }
        });
    });
    which is basically making my #wrap div stay fixed in view as the customer scrolls down my page.

    It then stops scrolling at 1233 pixels from bottom as per the "var visibleFoot" in above code...

    This is working great for me, all but the 1233 pixels - it can vary per page, and ideally i'd like the script to dynamically stop 30 pixels above my #reviews div.

    So i guess what i would ideally need is above code to calculate how many pixels, the top of the #review div is, from the bottom of the page.

    If anyone can help with this, would be great.

    kind regards
    James

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    Welcome to the forums.

    It shouldn't be too hard to calculate the position dynamically, but it would be much easier if we had the HTML to work with.

    Could you post a link to a page where I can see this in action?

  3. #3
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not positive this is what you want, but....

    the offsetTop property should tell you where a div begins

    var topValue = 0;
    topValue += reviewObj.offsetTop;

    if you subtract the value of the offsetHeight property of the "wrap" div (?) + 30 from the topValue (above), you should have what you want.
    var visibleFoot = ( topValue - (wrapObj.offsetHeight + 30) )
    Where the World Once Stood
    the blades of grass
    cut me still


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •