javascript - 跟踪滚动事件

时间:2012-10-21 03:28:17

标签: javascript jquery html

我有一个流氓滚动事件,每次使用Ajax加载div元素时,都会自动将页面滚动到顶部。我该如何追踪?我已经尝试在滚动事件处理程序中设置断点并查看callstack但是这并没有给我任何有用的东西,因为它只是显示它来自jquery,但我有兴趣知道哪个DOM元素导致滚动的事件。

一点背景,我在点击div元素时使用Ajax加载div元素(onclick)。注意这不是锚!!但每当我关闭或在页面底部,在加载div元素并将其添加到页面后,页面一直滚动到顶部。真烦人并试图追查发起那个流氓卷轴的元素......

感谢!

2 个答案:

答案 0 :(得分:0)

如果您不介意jQuery插件,请尝试查看waypoints plugin,这一点非常适合跟踪/处理滚动

答案 1 :(得分:0)

如果我理解得很好,你需要一个类似于此的代码,在这段代码中,当用户(通过滚动)到距离最后一个元素4000像素的距离时,我会从ajax显示新数据(在这种情况下是div .newDeal:last)(是一个很长的网站)。

$(document).ready(function() {

//global variable in this script to test if I already made the request ajax ( I dont 
//want to make continious ajax request)
var loaded=false;


$(window).bind('scroll', handlerScroll); //attach event to the scroll


})//end document ready

//FIRE AUTOSCROLL (FIRE REQUEST AJAX)
//*************************************

var handlerScroll=function (){

    //get position of the scroll
    var space=$('#listdeals div.newDeal:last').offset().top - $(window).scrollTop();
    //if that distance is less than (or the middle, or the bottom). fire the request ajax
    if  (space<= 4000  &&     jQuery('div.previousDeal').size()==0){  

        //disable scroll event
        $(window).unbind();



        //
        //build data post
        //
        var  parameters="actionAutoscroll=true&"+.........;

        //MAKE REQUEST AJAX
        //********************

        $.ajax({  
            url: "/offers",  
            type:'POST',  
            data: parameters,   
            success: process_previousDeals       
        });  //end ajax


        return false;

    }
}//end if  load previous deals depending of scroll

}//end handler


function process_previousDeals(results){
 //inject new content in the page
}
相关问题