简体   繁体   English

jQuery .scrollTop()屏幕高度

[英]jquery .scrollTop() screen height

I have been writing a small script that shortens documents that are being output on a JSON feed of mine. 我一直在写一个小的脚本,可以缩短在我的JSON提要上输出的文档。

I realised that with longer messages, on minimising it could skip a few and you'd have to scroll back to find where you were and so added a scroll function to take you back to the top of the message on minifying. 我意识到,对于较长的消息,在最小化时可能会跳过一些消息,因此您必须向后滚动以找到自己所在的位置,因此添加了滚动功能,以便在缩小消息时将您带回到消息的顶部。 This turned out to be quite annoying when it fired every single time - so I thought, why not make it fire only when the top of the element is above the screen? 事实证明,每次触发时都会令人讨厌-所以我想,为什么不让它仅在元素顶部位于屏幕上方时才触发?

And this is where I am stuck --> $(window).scrollTop(); 这就是我$(window).scrollTop(); -> $(window).scrollTop(); simply doesn't want to output the screen height for me, it's infuriating. 只是不想为我输出屏幕高度,这真是令人生气。 I tried with different browsers and the only one .scrollTop() worked for correctly was Internet Explorer. 我尝试使用其他浏览器,并且只能正常使用的一个.scrollTop()是Internet Explorer。

Here is the function below: 这是下面的函数:

function jexpand(id){
    var elm=$('#d'+jdesc[id].i); // element

    var ofs=elm.offset().top; // element height - works fine
    var top=$(window).scrollTop(); // Y U NO WORK?!!!

    if(jdesc[id].e==true){ // boolean to check whether to expand or contract
        jdesc[id].e=false; // change boolean flag
        if(ofs < top) $('html').animate({scrollTop:($('#'+jdesc[id].i).offset().top)-(20)+'px'},'slow'); // animate to top minus 20 pixels
        elm.html(jdesc[id].d.substring(0,347)+'...<br><div class="readmore"><span id="'+id+'">Show More</span></div>');
    }else{
        jdesc[id].e=true;
        elm.html(jdesc[id].d+'<br><div class="readmore"><span id="'+id+'">Show Less</span></div>');
    }
    $('.readmore span').click(function(){jexpand(this.id)}); // reset click trigger

    //alert(top+' <-> '+ofs);
}

When I uncomment the alert() at the bottom in a browser other than IE (I tried chrome, mozilla, chrome android, boat browser android) I get a message akin to: 当我在非IE的浏览器中取消注释底部的alert()时(我尝试使用chrome,mozilla,chrome android,boat boat android),我收到类似于以下消息:

[object Window] <-> 1077.5625

[object Window] is obviously not a number that can be greater or less than the element height! [object Window]显然不是一个大于或小于元素高度的数字! So what does this mean, is there another flag I need to ask of it? 那么,这意味着什么,我还需要问另一个标志吗? At first I assumed it might be the wrong element I was refering so tried top=$('body').scrollTop(); 起初,我认为这可能是我引用的错误元素,因此尝试了top=$('body').scrollTop(); , html , etc, I even tried using div wrapper elements but to no avail. html等,我什至尝试使用div包装器元素,但无济于事。

I am using jquery 1.11.0 and with 1.9.1 I had the same issue. 我正在使用jQuery 1.11.0,而在1.9.1中,我遇到了同样的问题。

Am I trying to return the screen top in the wrong way or have all my browsers gone loopy? 我是要尝试以错误的方式返回屏幕顶部,还是让所有浏览器都出现循环显示?

EDIT: 编辑:

Weirdly I've found an issue which may explain things a little, when I typed $(document).scrollTop() into a console it gave me the correct screen height however if I make a var top; 奇怪的是,我发现了一个可能会解释一些问题的问题,当我在控制台中键入$(document).scrollTop()时,它为我提供了正确的屏幕高度,但是如果我将其设为var top; outside of the function I get this error 'window.webkitStorageInfo' is deprecated. Please use 'navigator.webkitTemporaryStorage' or 'navigator.webkitPersistentStorage' instead. 在函数之外,我收到此错误'window.webkitStorageInfo' is deprecated. Please use 'navigator.webkitTemporaryStorage' or 'navigator.webkitPersistentStorage' instead. 'window.webkitStorageInfo' is deprecated. Please use 'navigator.webkitTemporaryStorage' or 'navigator.webkitPersistentStorage' instead. .

I am using a webkit borrowed from html5up and this is somehow interfering with the code. 我正在使用从html5up借用的Webkit,这以某种方式干扰了代码。 Now to find out what it is.... 现在找出它是什么...。

Sorry for the confusion, without this script everything works fine grr... 很抱歉造成混乱,如果没有此脚本,一切都将正常运行。

Try this way 试试这个

JS CODE: JS代码:

 $(window).scrollTop(0);  // this will scroll to top of the page

LIVE DEMO: 现场演示:

http://jsfiddle.net/dreamweiver/fZrz7/6/ http://jsfiddle.net/dreamweiver/fZrz7/6/

Happy Coding :) 快乐编码:)

What about $(document).scrollTop() ? $(document).scrollTop()呢? It always worked for me. 它总是为我工作。

Try smth like this.. 尝试这样的东西

if($(window).scrollTop()>500){
     elm.html(jdesc[id].d.substring(0,347)+'...<br><div class="readmore"><span id="'+id+'">Show More</span></div>');}
else{
     elm.html(jdesc[id].d+'<br><div class="readmore"><span id="'+id+'">Show Less</span></div>');
}

If work try next with 如果工作,请尝试与

var ofs=elm.offset().top;

Try this: 尝试这个:

$(document).height(); //returns window height

$(document).scrollTop(); //returns scroll position from top of document

$(selector)[0].scrollHeight;

$(document).prop('scrollHeight');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM