Javascript scrollTop()在移动视图或移动设备中不工作

Javascript scrollTop()在移动视图或移动设备中不工作,javascript,jquery,Javascript,Jquery,我想在页面加载时滚动到div的底部。我正在使用jQuery的animate()函数来完成此操作。问题是,我的代码在桌面视图上工作,但如果我将其更改为移动视图,则我的代码不工作 $(“.chat msg list”).animate({ scrollTop:$(“.chat msg list”).prop(“scrollHeight”) }, 1000); jQuery: $(".chat-msg-list").animate({ scrollTop: $(".chat-msg-l

我想在页面加载时滚动到div的底部。我正在使用jQuery的
animate()
函数来完成此操作。问题是,我的代码在桌面视图上工作,但如果我将其更改为移动视图,则我的代码不工作

$(“.chat msg list”).animate({
scrollTop:$(“.chat msg list”).prop(“scrollHeight”)
}, 1000);
jQuery:

$(".chat-msg-list").animate({
        scrollTop: $(".chat-msg-list").offset().top},
}, 1000);
Javascript:

 function getElementY(query) {
  return window.pageYOffset + document.querySelector(query).getBoundingClientRect().top
}

function doScrolling(element, duration) {
    var startingY = window.pageYOffset
    var elementY = getElementY(element)
    // If element is close to page's bottom then window will scroll only to some position above the element.
    var targetY = document.body.scrollHeight - elementY < window.innerHeight ? document.body.scrollHeight - window.innerHeight : elementY
    var diff = targetY - startingY
    // Easing function: easeInOutCubic
    // From: https://gist.github.com/gre/1650294
    var easing = function (t) { return t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1 }
    var start

    if (!diff) return

    // Bootstrap our animation - it will get called right before next frame shall be rendered.
    window.requestAnimationFrame(function step(timestamp) {
        if (!start) start = timestamp
        // Elapsed miliseconds since start of scrolling.
        var time = timestamp - start
            // Get percent of completion in range [0, 1].
        var percent = Math.min(time / duration, 1)
        // Apply the easing.
        // It can cause bad-looking slow frames in browser performance tool, so be careful.
        percent = easing(percent)

        window.scrollTo(0, startingY + diff * percent)

            // Proceed with animation as long as we wanted it to.
        if (time < duration) {
          window.requestAnimationFrame(step)
        }
    })
}

//Apply event handlers. Example of firing the scrolling mechanism.
doScrolling('#section1', 1000)
函数getElementY(查询){ return window.pageYOffset+document.querySelector(query).getBoundingClientRect().top } 功能文件归档(要素、持续时间){ var startingY=window.pageYOffset var elementY=getElementY(元素) //若元素靠近页面底部,那个么窗口将只滚动到元素上方的某个位置。 var targetY=document.body.scrollHeight-elementY
$(".chat-msg-list").animate({
        scrollTop: $(".chat-msg-list").offset().top},
}, 1000);
Javascript:

 function getElementY(query) {
  return window.pageYOffset + document.querySelector(query).getBoundingClientRect().top
}

function doScrolling(element, duration) {
    var startingY = window.pageYOffset
    var elementY = getElementY(element)
    // If element is close to page's bottom then window will scroll only to some position above the element.
    var targetY = document.body.scrollHeight - elementY < window.innerHeight ? document.body.scrollHeight - window.innerHeight : elementY
    var diff = targetY - startingY
    // Easing function: easeInOutCubic
    // From: https://gist.github.com/gre/1650294
    var easing = function (t) { return t<.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1 }
    var start

    if (!diff) return

    // Bootstrap our animation - it will get called right before next frame shall be rendered.
    window.requestAnimationFrame(function step(timestamp) {
        if (!start) start = timestamp
        // Elapsed miliseconds since start of scrolling.
        var time = timestamp - start
            // Get percent of completion in range [0, 1].
        var percent = Math.min(time / duration, 1)
        // Apply the easing.
        // It can cause bad-looking slow frames in browser performance tool, so be careful.
        percent = easing(percent)

        window.scrollTo(0, startingY + diff * percent)

            // Proceed with animation as long as we wanted it to.
        if (time < duration) {
          window.requestAnimationFrame(step)
        }
    })
}

//Apply event handlers. Example of firing the scrolling mechanism.
doScrolling('#section1', 1000)
函数getElementY(查询){ return window.pageYOffset+document.querySelector(query).getBoundingClientRect().top } 功能文件归档(要素、持续时间){ var startingY=window.pageYOffset var elementY=getElementY(元素) //若元素靠近页面底部,那个么窗口将只滚动到元素上方的某个位置。 var targetY=document.body.scrollHeight-elementYvar=功能(t){return t问题是否出现在桌面浏览器或移动设备浏览器的移动视图中?@gypsyCoder问题出现在两种情况下的桌面浏览器或移动设备浏览器中。您可以尝试以下方法:问题是否出现在桌面浏览器或移动设备浏览器的移动视图中?@gypsyCoder问题出现在两种情况下的桌面浏览器或移动设备浏览器中移动设备的浏览器您可以尝试以下方法: