Solved

jQuery Window Scrolling Issue

Posted on 2015-02-11
3
163 Views
Last Modified: 2015-02-11
I have a page that initially loads 20 product. When the user hits the bottom of the page, it loads 20 more and appends it to the existing product results. Due to a problem with IE9 not performing this adequately, we decided to load more product when a user scrolls 25 pixels above the bottom of the page.

The problem is, if the user scrolls too slow, the even will fire multiple times and load 40, 60, 80, 120, even 160 product, depending on how many times the event fires. If they scroll fast, it typically works.

I think the event is firing too fast, and I'm not sure how to get the jQuery to do a single load only once if a user scrolls slowly.

I messed around with bind() and unbind() but couldn't get it to work quite right.

Does anyone have any thoughts on how to make this work (or have another solution entirely)?

    $(window).scroll(function () {
        var sum = $(window).scrollTop() + ($(window).height() * 1) + 25;
        var origDocHeight = $(document).height();
        var docHeight = $(document).height();


        // Load new data
        /////////////////////////////////////////////////
        if (sum >= docHeight) {

            // Loading product
            var thisURL = '/Products/moreResults.cfm?version=' + version;
            $.get(thisURL, {}, function (data) { $(".scrollContainer").append(data) })

        } // END IF
    });

Open in new window

0
Comment
Question by:infotechelg
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 40604205
Hi,
just (an untested) quirk solution:
you store the size of the last load and add a fixed size in pixel which will normally take the space of 20 products. New products will only be loaded if the new position is greater than this sum.

var lastScrollEventPosition = 0;

$(window).scroll(function () {
        var sum = $(window).scrollTop() + ($(window).height() * 1) + 25;
        var origDocHeight = $(document).height();
        var docHeight = $(document).height();


        // Load new data
        /////////////////////////////////////////////////
        if (sum >= docHeight && sum>(lastScrollEventPosition+200)) {
            // Set the new position
           lastScrollEventPosition = sum;
            // Loading product
            var thisURL = '/Products/moreResults.cfm?version=' + version;
            $.get(thisURL, {}, function (data) { $(".scrollContainer").append(data) })

        } // END IF
    });

Open in new window

As I said, not tested but a possible quick good shot.
HTH
Rainer
0
 

Author Comment

by:infotechelg
ID: 40604214
Thanks Rainer, we'll give that a shot.
0
 

Author Closing Comment

by:infotechelg
ID: 40604244
Brilliant! You're the man. That worked! Thanks a bunch.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now