Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ';无穷无尽的卷轴';在网页上的div中_Javascript_Jquery_Html_Css_Facebook - Fatal编程技术网

Javascript ';无穷无尽的卷轴';在网页上的div中

Javascript ';无穷无尽的卷轴';在网页上的div中,javascript,jquery,html,css,facebook,Javascript,Jquery,Html,Css,Facebook,我想在一个页面中创建一个包含无限滚动内容的div 为了识别可滚动内容的结尾,以便从数据库加载更多数据,我编写了以下代码: $(window).scroll(function(){ var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height(); var scrolltrigger = 0.95; if ((wintop/(docheight

我想在一个页面中创建一个包含无限滚动内容的div

为了识别可滚动内容的结尾,以便从数据库加载更多数据,我编写了以下代码:

$(window).scroll(function(){

  var wintop = $(window).scrollTop(), docheight = $(document).height(), winheight = $(window).height();
  var  scrolltrigger = 0.95;

  if  ((wintop/(docheight-winheight)) > scrolltrigger) {
     lastAddedLiveFunc();
  }
});
问题:以下代码用于创建适合整个网页的无休止卷轴,但如何为网页中的“div”编写相同的卷轴?

(提示:一个完全相似的东西是Facebook上的股票代码,右边实时显示你朋友最近的活动。)

您可以使用div而不是window

比如说

$('#yourdiv').scrollTop()  //  etc

原理完全相同。div将是您的浏览器视口,div内容是您的文档。你只需要交换

  • $(窗口).scroll()
    -->
    $(“#someDiv”).scroll()
  • $(窗口).scrollTop()
    -->
    $(“#someDiv”).scrollTop()
  • $(document).height()
    -->
    $(“#someDiv”)[0]。滚动高度
  • $(窗口).height()
    -->(“#someDiv”).height()
它应该会起作用

注意:

  • scrollHeight
    是一个属性,而不是一个函数
  • scrollHeight
    不是jQuery,因此需要从选择器的实际javascript元素获取它,因此添加了
    [0]
    。另一种方法是使用$(“#someDiv”).prop(“滚动高度”)
    • 使用此

      // get box div position
      var box = document.getElementById('box').offsetTop;
      
      window.onscroll = function(){
      
          // get current scroll position
          var scroll_top = document.body.scrollTop || document.documentElement.scrollTop;
      
          document.getElementById('scbox').innerText = scroll_top + ' ' + box;
      
          // if current scroll position >= box div position then box position fixed
          if(scroll_top >= box)
              document.getElementById('box').style.position = 'fixed';
          else
              document.getElementById('box').style.position = 'relative';    
      }
      

      var scroll_top=document.body.scrollTop | | document.documentElement.scrollTop;你的答案看起来很有趣!请你举个例子好吗?!你知道jQuery中scrollHeight的等效值吗?!我知道我可以使用jQuery1.7中发布的:$(“#dvContent”).prop(“scrollHeight”);但是你知道任何纯jQuery等价物吗?不要认为它存在$(#dvContent”).prop(“scrollHeight”)或$(#dvContent”)[0]。scrollHeight似乎是您唯一的选项。我尝试过使用它,但它不起作用。请看一看,告诉我哪里出了问题,挽救了我的生命:(?!你将滚动事件侦听器添加到了窗口,而不是实际的div。你是个很棒的朋友!非常感谢;)
      // get box div position
      var box = document.getElementById('box').offsetTop;
      
      window.onscroll = function(){
      
          // get current scroll position
          var scroll_top = document.body.scrollTop || document.documentElement.scrollTop;
      
          document.getElementById('scbox').innerText = scroll_top + ' ' + box;
      
          // if current scroll position >= box div position then box position fixed
          if(scroll_top >= box)
              document.getElementById('box').style.position = 'fixed';
          else
              document.getElementById('box').style.position = 'relative';    
      }