2016-05-30 207 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