Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

scrollTop 值一直为零,且不能赋值

sendMessage:function(){
            var _this = this;
            var userWords = document.createElement('li');
            var ulTag = document.querySelector('.list-box');

            userWords.innerHTML = "<div class='contact-box__send'>"+ _this.userMessage +"</div>"
            userWords.setAttribute('class','talk users-words contact-box clearfix');
            ulTag.appendChild(userWords);
            ulTag.scrollTop = ulTag.scrollHeight; //此处不能赋值
            console.log(ulTag.scrollTop,ulTag.scrollHeight); 
        }
        
        
        我希望达到插入一个元素,内容区域自动滚动到底部

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

scrollTop设置的是滚动条的位置。
你插入内容的区域应该是随着插入变高的,没有滚动条,滚动条大约在body上
因此需要设置$('body').scrollTop


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...