Javascript 带转换持续时间的Java脚本滚动顶部
这是我的职责。我找不到如何在其中添加过渡持续时间。它直接把我带到山顶。语法是我要找的。idk非常了解jsJavascript 带转换持续时间的Java脚本滚动顶部,javascript,Javascript,这是我的职责。我找不到如何在其中添加过渡持续时间。它直接把我带到山顶。语法是我要找的。idk非常了解js function topFunction() { document.body.scrollTop=0 ; document.documentElement.scrollTop =0 ; } 您可以使用下面的代码添加转换持续时间 function topFunction() { currentYOffset = self.pageYOffset; initYOffset =
function topFunction() {
document.body.scrollTop=0 ;
document.documentElement.scrollTop =0 ;
}
您可以使用下面的代码添加转换持续时间
function topFunction() {
currentYOffset = self.pageYOffset;
initYOffset = currentYOffset;
var intervalId = setInterval(function(){
currentYOffset -= initYOffset*0.05;
document.body.scrollTop = currentYOffset ;
document.documentElement.scrollTop = currentYOffset;
if(self.pageYOffset == 0){
clearInterval(intervalId);
}
}, 20);
}
可以通过编辑“间隔”的值来设置过渡的平滑度。我已将其设置为20。您可以使用以下代码添加转换持续时间
function topFunction() {
currentYOffset = self.pageYOffset;
initYOffset = currentYOffset;
var intervalId = setInterval(function(){
currentYOffset -= initYOffset*0.05;
document.body.scrollTop = currentYOffset ;
document.documentElement.scrollTop = currentYOffset;
if(self.pageYOffset == 0){
clearInterval(intervalId);
}
}, 20);
}
可以通过编辑“间隔”的值来设置过渡的平滑度。我已将其设置为20。这是一个jQuery解决方案,使用函数 该函数使您能够更改动画时间以及应用动画的选择器,以便您也可以滚动其他元素
/**
* Scroll to a given point on the page with animation
*
* @param {int} scrollValue - The top position to scroll to
* @param {int} [animationTime] - Speed of the animation (in milliseconds)
* @param {string} [selector] - Which element to scroll. By default, it is the html/body
*/
function animateScrollTo(scrollValue, animationTime, selector){
if(typeof animationTime === "undefined"){
animationTime = 520;
}
if(typeof selector === "undefined"){
selector = "html, body";
}
jQuery(selector).animate({scrollTop: scrollValue}, animationTime);
}
下面是一个jQuery解决方案,使用函数 该函数使您能够更改动画时间以及应用动画的选择器,以便您也可以滚动其他元素
/**
* Scroll to a given point on the page with animation
*
* @param {int} scrollValue - The top position to scroll to
* @param {int} [animationTime] - Speed of the animation (in milliseconds)
* @param {string} [selector] - Which element to scroll. By default, it is the html/body
*/
function animateScrollTo(scrollValue, animationTime, selector){
if(typeof animationTime === "undefined"){
animationTime = 520;
}
if(typeof selector === "undefined"){
selector = "html, body";
}
jQuery(selector).animate({scrollTop: scrollValue}, animationTime);
}
只需使用此属性和document.getElementById('some-id')。scrollTop=0
将用于平滑过渡/动画
只需使用此属性和
文档。getElementById('some-id')。scrollTop=0将用于平滑过渡/动画。尝试使用此属性代替函数:
window.scrollTo({
top: 100,
left: 100,
behavior: 'smooth'
});
尝试使用此功能代替您的功能:
window.scrollTo({
top: 100,
left: 100,
behavior: 'smooth'
});
您需要学习CSS(转换),或者像jQuery这样的库,以便通过转换实现您想要的功能。我知道CSS good@JaromandaX的可能副本。我不知道的是如何在js中嵌入它。在我的职责范围内。因为这是将它滚动到我的页面顶部的方法。可能的重复事实上,我不认为CSS转换适用,所以你需要学习CSS(转换),或者像jQuery这样的库来做你想要的转换。可能的重复我知道CSS good@JaromandaX。我不知道的是如何在js中嵌入它。在我的职责范围内。因为它是滚动到我页面顶部的东西。实际上,可能是重复的,我不认为CSS转换适用,对不起,这什么时候起作用?当我这样做时,它似乎没有帮助,甚至在Chrometh中也没有。这正是它对我的工作方式document.getElementById('chat-content')。scrollTop=0
什么时候起作用?当我这样做时,它似乎没有帮助,甚至在Chrometh中也没有。这正是它对我的工作方式document.getElementById('chat-content')。scrollTop=0