简体   繁体   English

无限滚动跳到页面顶部

[英]Infinite scroll jumps to top of page

I'm using infinite scroll with masonry everything works fine and lines up correctly. 我对砖石使用了无限滚动,一切正常,并且排列正确。 I have a button that when i click it makes that div bigger and shows extra content. 我有一个按钮,当我单击它会使该div变大并显示更多内容。 the button works and loads fine when the page initially loads but when i scroll down and infinite loads new items and if i click the button to show more it jumps to the top of the screen. 当页面最初加载时,该按钮工作并加载良好,但是当我向下滚动并无限加载新项目时,如果我单击按钮以显示更多,它将跳转到屏幕顶部。

I'm guessing i have to do some callback? 我猜我必须做一些回调吗? Im kinda confused on this. 我对此有些困惑。 How should I approach it? 我应该如何处理?

this is the code im using: 这是我使用的代码:

$(function(){

$(".fancybox").fancybox();
   var $container = $('.main_containera');
   $container.imagesLoaded(function(){
  $container.masonry({
    itemSelector: '.item1',
    columnWidth: 0
  });
});


var nextSelector = '.pagination-next a';
var origNextUrl = $(nextSelector).attr('href');
var offsetRegex = /(offset=)([0-9]+)/;
var offset = origNextUrl.match(offsetRegex)[2];


$container.infinitescroll({
  navSelector  : '.paginate',    // selector for the paged navigation 
  nextSelector : '.pagination-next a',  // selector for the NEXT link (to page 2)
  itemSelector : '.item1',     // selector for all items you'll retrieve
  loading: {
      finishedMsg: 'No more pages to load.',
      img: 'http://i.imgur.com/6RMhx.gif'
    }
  },
  // trigger Masonry as a callback
  function( newElements ) {
    // hide new items while they are loading
    var $newElems = $( newElements ).css({ opacity: 0 });
    // ensure that images load before adding to masonry layout
    $newElems.imagesLoaded(function(){
      // show elems now they're ready
      $newElems.animate({ opacity: 1 });
      $container.masonry( 'appended', $newElems, true );

    });
  }
);


$('.comment_tr').click(function (e) {
    $(this).toggleClass('disabled');
    $(this).parent().parent().parent().find('form').slideToggle(250, function () {
        $('.main_containera').masonry();
    });
    e.preventDefault();
}); 

  });

Try changing 尝试改变

$('.comment_tr').click(function (e) {
    $(this).toggleClass('disabled');
    $(this).parent().parent().parent().find('form').slideToggle(250, function () {
        $('.main_containera').masonry();
    });
    e.preventDefault();
});

to

$('.comment_tr').click(function (e) {
    $(this).toggleClass('disabled');
    $(this).parent().parent().parent().find('form').slideToggle(250, function () {
        $('.main_containera').masonry();
    });
    e.preventDefault();
    e.stopPropagation( );
});

The click event may be propagating up to another link element which has an empty href or one that returns the user to the same page which usually just takes you back to the top. click事件可能正在传播到另一个链接元素,该链接元素具有空的href或使用户返回同一页面(通常只是将您带回顶部)。 Alternatively, you could replace e.preventDefault(); 或者,您可以替换e.preventDefault(); and e.stopPropagation(); e.stopPropagation(); with return false; return false; .

It's hard to say for sure that that's the issue without seeing the HTML though. 虽然没有看到HTML,但是很难确定那是问题所在。

Could you post the HTML if that change doesn't work? 如果更改无效,您可以发布HTML吗?

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

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