繁体   English   中英

scrollTop在Firefox和IE中不起作用

[英]scrollTop not working in firefox and IE

我在firefox和IE中使用scrolltop时遇到一些问题,我在代码中使用了超过2次scrolltop,在第一部分中它起作用了,但是在第二部分中却没有两个箭头“ next”和“ prev”,当单击它们时,它们会滚动到具体部分,我找不到解决方法?

jQuery的:

var lchiled=$("ul#portfolio li").last();
var fchiled=$("ul#portfolio li").first();
$('li.section').first();
$("ul#portfolio li:first-child").addClass("current");

$('a.display').on('click', function(e) {
    e.preventDefault();

    var t = $(this).attr('name');

    that = $(this);


    if (t === 'next') {
        if($('.current').next('.section').length==0)
            var $next = $('li.section').first();
        else
            var $next = $('.current').next('.section');

        var top = $next.offset().top -65;
        $('.current').removeClass('current');

        $('body,html').animate({
        scrollTop: top,
        }, 

        function () {
        $next.addClass('current');
        // alert(top);
        });
    }
    else if (t === 'prev' && $('.current').prev('li.section').length > 0) {
        var $prev = $('.current').prev('.section');
        var top = $prev.offset().top -65;
        $('.current').removeClass('current');

        $('body').animate({
             scrollTop: top,     
        }, function () {
            $prev.addClass('current');
        });
    } 

});

html:

<div id="container">

   <ul id="portfolio" class="clearfix">

   </ul>

</div>

lis是使用jquery代码动态生成的

一定是这样

$('body,html').animate({
         scrollTop: top,     
    }, function () {
        $prev.addClass('current');
    });

装的

$('body').animate({
         scrollTop: top,     
    }, function () {
        $prev.addClass('current');
    });

我忘记更新上一部分,所以发生了此问题。

您可以在两个地方的scrollTop上使用.animate 在其中,您(正确)使用html,body作为选择器。 另一方面,您仅使用body 而且您想知道为什么它在某些浏览器中不起作用;)

尝试var offset = $(window).scrollTop(); 这个 。

您可以使用window.scrollTo(x,y)

暂无
暂无

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

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