admin管理员组文章数量:1435859
this is my function. i cant find how to add transition duration in it. it just takes me straight upto top. syntax is what i'm looking for. idk much about js.
function topFunction() {
document.body.scrollTop=0 ;
document.documentElement.scrollTop =0 ;
}
this is my function. i cant find how to add transition duration in it. it just takes me straight upto top. syntax is what i'm looking for. idk much about js.
function topFunction() {
document.body.scrollTop=0 ;
document.documentElement.scrollTop =0 ;
}
Share
Improve this question
edited Aug 11, 2017 at 6:42
Jaromanda X
1
asked Aug 11, 2017 at 6:41
Kamran HashmiKamran Hashmi
611 silver badge7 bronze badges
7
- you'll need to learn CSS (transitions), or some library like jQuery to do what you want with a transition – Jaromanda X Commented Aug 11, 2017 at 6:44
- 6 Possible duplicate of Cross browser JavaScript (not jQuery…) scroll to top animation – Mihai Pantea Commented Aug 11, 2017 at 6:44
- i know css good @JaromandaX. what i dont know is how to embed it in js. in my function. because it's what scrolling it to the top of my page. – Kamran Hashmi Commented Aug 11, 2017 at 6:46
- Possible duplicate of Cross browser JavaScript (not jQuery...) scroll to top animation – Odin Thunder Commented Aug 11, 2017 at 6:46
- actually, I don't think CSS transitions apply, sorry – Jaromanda X Commented Aug 11, 2017 at 6:47
4 Answers
Reset to default 3Try using this in place of your function:
window.scrollTo({
top: 100,
left: 100,
behavior: 'smooth'
});
Reference
You can add transition duration by using below code.
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);
}
You can set smoothness of the transition by editing the value of interval. I have set it as 20.
Here is a jQuery solution, using the jQuery.animate function.
The function gives you the ability to change the animation time, as well as the selector on which the animation is being applied, so that you can scroll other elements, too.
/**
* 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);
}
body {
scroll-behavior: smooth;
}
Just use this property and document.getElementById('some-id').scrollTop = 0
will work with smooth transition/animation.
本文标签: javascriptJava Script scroll top with transition durationStack Overflow
版权声明:本文标题:javascript - Java Script scroll top with transition duration - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745656188a2668721.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论