滚动条滚动到最底部的方法总结

1,jquery

获取scrollHeight:

$("div")[0].scrollHeight

$('div').prop('scrollHeight');

设置scrollTop

$(".chat-con").scrollTop(h);//这里没有引号,注意

2,js

var div = document.getElementById('scrolldIV');

div.innerHTML = div.innerHTML + 'time_' + now.getTime() + '<br />';

div.scrollTop = div.scrollHeight;

3,判断元素是否在页面中显示,浏览历史的时候,不到底部

function isNewInWindow(){
            // var div = document.getElementsByTagName("div");
            // div_length = div.length-6;

            // var l=$(".chat-con").children("div:last");
            var l=document.getElementsByClassName("chat-item");         
            if(isInWindow(l[l.length-1])){
                return true;
            }
            return false;
    }
    
    /*判定元素是否在界面内*/
    function isInWindow(x){     
         var h=$(".chat-con")[0].clientHeight;
            if(x.getBoundingClientRect().top >2*h ){
                // 元素低于当前界面
                return false;
            }
            else if(x.getBoundingClientRect().bottom < 0){
                // 元素高于当前界面
                return false;
            }
            return true;
    }

猜你喜欢

转载自www.cnblogs.com/bluestear/p/9418575.html