1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

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

Discussion in 'jQuery' started by James McCann, Nov 26, 2013.

  1. #1
    hi all,

    i'm a novice at scripting etc, but have been tying the below 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"
                });
            }
        });
    });
    Code (markup):

    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
     
    James McCann, Nov 26, 2013 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Uhm... stupid question...

    Why not just used position:fixed instead of using javascript to do CSS' job?!? Doesn't work in IE6/lower, but at this point who gives a ****...

    Just saying... NOT that fixing elements on the screen like that is a good idea in the first place given how crappy that is on small screens like mobile or tablets... oh noes, a user has to scroll back up to something, not that...
     
    deathshadow, Nov 26, 2013 IP