English 中文(简体)
Load ajax, roll升到80%
原标题:Load ajax when scroll reaches 80%

I am using the following code which is working when the scroll bar reaches the botttom,

if($(window).scrollTop() == $(document).height() - $(window).height()){

然而,我想,当一至70%的输电量不超过100分时,就会发射炭 the。

最佳回答

如果你目前的检查是在向下页投射时发射的,你可以尝试一些基本的算术:

if ($(window).scrollTop() >= ($(document).height() - $(window).height())*0.7){
                                          //where 0.7 corresponds to 70% --^

确保增加检查,避免同时发生多起火灾 Ajax要求,如果你已经这样做的话。

这不是问题的范围,但如果你想一例说明如何防止多重要求同时发射:

Declare a global var, e.g. process

Then incorporate it in your function:

if (processing)
    return false;

if ($(window).scrollTop() >= ($(document).height() - $(window).height())*0.7){
    processing = true; //sets a processing AJAX request flag
    $.post("url",  <params> , function(data){ //or $.ajax, $.get, $.load etc.
        //load the content to your div
        processing = false; //resets the ajax flag once the callback concludes
    });
}

这是一种简单的例子,即如果阿贾克斯积极要求你履行保姆职能,就使用var来跟踪,而且这并不影响你可能提出的任何其他同意的阿贾克斯要求。

Edit: JSFiddle example

请注意,如果使用一定百分比来衡量文件高度,可能是一种坏的想法,因为文件高度将每增加一次,使Ajax的要求比上页(绝对明智)更远。

d 我建议使用固定价值加以抵消,以防(200-700或这样):

if ($(window).scrollTop() >= $(document).height() - $(window).height() - 700){
                                 // pixels offset from screen bottom   --^

例:JSFiddle

<><>Edit>: 在第一部法典中以百分数转载这一问题,将第50条<代码>div。 当你装载下个<代码>div时,该编号在总文件高度上只加2%,这意味着一旦你将2%的封顶推回70%,将立即启动下一个要求。 在我的固定例子中,只有在用户处于从屏幕底线到底的确定绝对颜料时,界定的底线抵销才会装载新的内容。

问题回答

快速探寻<条码>的植被百分比载于,由此得出以下结果:,第页,这是第一种结果(根据以下代码,你想要做的多或少)。 我同你一样,在提出之前,没有试图进行任何研究。

$(document).scroll(function(e){

    // grab the scroll amount and the window height
    var scrollAmount = $(window).scrollTop();
    var documentHeight = $(document).height();

    // calculate the percentage the user has scrolled down the page
    var scrollPercent = (scrollAmount / documentHeight) * 100;

    if(scrollPercent > 50) {
        // run a function called doSomething
       doSomething();
    }

    function doSomething() { 

        // do something when a user gets 50% of the way down my page

    }

});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签