2016-05-30 135 views
0

我正在聊天服务上工作,我需要该网站滚动到查特底部,如果有人向上滚动,则向下滚动的脚本将停止。 我在id #chat_innhold的div中聊天,这里是我的尝试。当接近底部时滚动到div的底部

window.setInterval(function scrolled(o) 
{ 
     if(o.offsetHeight + o.scrollTop > o.scrollHeight - 75) 
     { 
      window.setInterval=function() 
       { 
       return false; 
       } 
     } 
     else 
     { 
      window.setInterval=function() 
       { 
       $("html, #chat_innhold").animate({ scrollTop: $('#chat_innhold').prop("scrollHeight")}, 'slow'); 
       } 
     } 
}, 1000); 

当我是75像素以上的底部,一个想停止滚动底部功能。

回答

0

你为什么重写window.setInterval?

+0

它只是坏的编码...林很新的JavaScript,和我看到的错误。我认为它应该是两个不同的功能或类似 –

0

如果我理解正确的,你需要的东西一样,

<div class="wrapper"> 
    <button>Go to bottom</button> 
    <button class="go-top">Go to top</button> 
</div> 

body, 
.wrapper { 
    margin: 0; 
    padding: 0; 
} 
.wrapper { 
    position: relative; 
    height: 1000px; 
} 
.go-top { 
    bottom: 0; 
    position: absolute; 
    left: 0; 
} 

var elem = $('.wrapper')[0]; 
if (elem.addEventListener) { 
    if ('onWheel' in document) { 
    // IE9+, FF17+, Ch31+ 
    elem.addEventListener("wheel", onWheel); 
    } else if ('onmousewheel' in document) { 
    // old variant 
    elem.addEventListener("mousewheel", onWheel); 
    } else { 
    // Firefox < 17 
    elem.addEventListener("MozMousePixelScroll", onWheel); 
    } 
} else { // IE8- 
    elem.attachEvent("onmousewheel", onWheel); 
} 

function onWheel(e) { 
    e = e || window.event; 
    var delta = e.deltaY || e.detail || e.wheelDelta; 
    if(delta < 0 || delta > 0) { 
     $('html, body').stop(); 
    } 
} 

var deltaWindowBodyHeight = $('body').height() - $(window).height(); 

function goToBottom() { 
    $('html, body').animate({ 
     scrollTop: deltaWindowBodyHeight - 75 
    }, 1000); 
} 
function goToTop() { 
    $('html, body').animate({ 
     scrollTop: 0 
    }, 1000); 
} 
$('button').click(function() { 
if($(this).hasClass('go-top')) { 
    goToTop(); 
} else { 
    goToBottom(); 
} 
}); 

JSFiddle example

+0

我的意思是滚动条总是在最下面,除非用户开始向上滚动。 由于它的聊天服务,我希望所有新的聊天信息在新信息出现时显示在底部,并且如果用户向上滚动,该功能将停止总是在底部脚本上。希望它使sence :) –

+0

这是我工作的脚本,但我想停止功能,当我使用滚轮: –

+0