简体   繁体   English

使用 JavaScript 滚动到页面顶部?

[英]Scroll to the top of the page using JavaScript?

How do I scroll to the top of the page using JavaScript?如何使用 JavaScript 滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.立即跳转到页面顶部的滚动条也是可取的,因为我不希望实现平滑滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

Scroll to top of page with animation :使用动画滚动到页面顶部

window.scrollTo({ top: 0, behavior: 'smooth' });

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

Smooth scrolling & animation with vanilla Javascript , without jQuery使用 vanilla Javascript平滑滚动和动画,无需jQuery

 // Get the el let topBtn = document.querySelector(".top-btn"); // On Click, Scroll to the page's top, replace 'smooth' with 'instant' if you don't want smooth scrolling topBtn.onclick = () => window.scrollTo({ top: 0, behavior: "smooth" }); // On scroll, Show/Hide the btn with animation window.onscroll = () => window.scrollY > 500 ? topBtn.style.opacity = 1 : topBtn.style.opacity = 0
 body { background-color: #111; height: 5000px; } .top-btn { all: unset; position: fixed; right: 20px; bottom: 20px; cursor: pointer; transform:scale(1.8); opacity: 0; transition: .3s; }
 <button class="top-btn">🔝</button>

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

How do I scroll to the top of the page using JavaScript?如何使用JavaScript滚动到页面顶部? The scrollbar instantly jumping to the top of the page is desirable too as I'm not looking to achieve smooth scrolling.滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

function scrolltop() {

    var offset = 220;
    var duration = 500;

    jQuery(window).scroll(function() {
        if (jQuery(this).scrollTop() > offset) {
            jQuery('#back-to-top').fadeIn(duration);
        } else {
            jQuery('#back-to-top').fadeOut(duration);
        }
    });

    jQuery('#back-to-top').click(function(event) {
        event.preventDefault();
        jQuery('html, body').animate({scrollTop: 0}, duration);
        return false;
    });
}

None of the answers above will work in SharePoint 2016. 以上所有答案均不适用于SharePoint 2016。

It has to be done like this : https://sharepoint.stackexchange.com/questions/195870/ 它必须像这样完成: https : //sharepoint.stackexchange.com/questions/195870/

var w = document.getElementById("s4-workspace");
w.scrollTop = 0;

Active all Browser. 激活所有浏览器。 Good luck 祝好运

var process;
        var delay = 50; //milisecond scroll top
        var scrollPixel = 20; //pixel U want to change after milisecond
        //Fix Undefine pageofset when using IE 8 below;
        function getPapeYOfSet() {
            var yOfSet = (typeof (window.pageYOffset) === "number") ? window.pageYOffset : document.documentElement.scrollTop;
            return yOfSet;
        }



        function backToTop() {
            process = setInterval(function () {
                var yOfSet = getPapeYOfSet();
                if (yOfSet === 0) {
                    clearInterval(process);
                } else {
                    window.scrollBy(0, -scrollPixel);
                }
            }, delay);
        }

Try this尝试这个

<script>
  $(function(){
   $('a').click(function(){
    var href =$(this).attr("href");
   $('body, html').animate({
     scrollTop: $(href).offset().top
     }, 1000)
  });
 });
 </script>

If you'd like to scroll to any element with an ID, try this:如果您想滚动到任何带有 ID 的元素,请尝试以下操作:

$('a[href^="#"]').bind('click.smoothscroll',function (e) {
    e.preventDefault();
    var target = this.hash;
    $target = $(target);
    $('html, body').stop().animate({
        'scrollTop': $target.offset().top
    }, 700, 'swing', function () {
        window.location.hash = target;
    });
});``

document.getElementById("elem").scrollIntoView();

There is no need to javascript , event if you wanted to animate the scroll action!如果您想为滚动动作设置动画,则无需 javascript , event !

CSS: CSS:

html {
    scroll-behavior: smooth;
}

HTML: HTML:

<html>
  <body>
     <a id="top"></a>
     <!-- your document -->
     <a href="#top">Jump to top of page</a>
  </body>
</html>
document.getElementsByTagName('html')[0].scrollIntoView({ behavior: "smooth" });

When top scroll is top less than limit bottom and bottom to top scroll Header is Sticky.当顶部滚动顶部小于限制底部和底部到顶部滚动标题是粘性的。 Below See Fiddle Example.下面参见小提琴示例。

var lastScroll = 0;

$(document).ready(function($) {

$(window).scroll(function(){

 setTimeout(function() { 
    var scroll = $(window).scrollTop();
    if (scroll > lastScroll) {

        $("header").removeClass("menu-sticky");

    } 
    if (scroll == 0) {
    $("header").removeClass("menu-sticky");

    }
    else if (scroll < lastScroll - 5) {


        $("header").addClass("menu-sticky");

    }
    lastScroll = scroll;
    },0);
    });
   });

https://jsfiddle.net/memdumusaib/d52xcLm3/ https://jsfiddle.net/memdumusaib/d52xcLm3/

Just Try, no need other plugin / frameworks只需尝试,无需其他插件/框架

 document.getElementById("jarscroolbtn").addEventListener("click", jarscrollfunction); function jarscrollfunction() { var body = document.body; // For Safari var html = document.documentElement; // Chrome, Firefox, IE and Opera body.scrollTop = 0; html.scrollTop = 0; }
 <button id="jarscroolbtn">Scroll contents</button>
 html, body { scroll-behavior: smooth; }

Shortest最短的

location='#'

This solution is improvement of pollirrata answer and have some drawback: no smooth scroll and change page location, but is shortest此解决方案是对pollirrata 答案的改进,有一些缺点:无法平滑滚动和更改页面位置,但最短

Back to Top with AplineJS and TailwindCSS:使用 AplineJS 和 TailwindCSS 返回顶部:

<button
    x-cloak
    x-data="{scroll : false}"
    @scroll.window="document.documentElement.scrollTop > 20 ? scroll = true : scroll = false"
    x-show="scroll" @click="window.scrollTo({top: 0, behavior: 'smooth'})"
    type="button"
    data-mdb-ripple="true"
    data-mdb-ripple-color="light"
    class="fixed inline-block p-3 text-xs font-medium leading-tight text-white uppercase transition duration-150 ease-in-out bg-blue-600 rounded-full shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg bottom-5 right-5"
    id="btn-back-to-top"
    x-transition.opacity
>
    <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor">
        <path fill-rule="evenodd" d="M3.293 9.707a1 1 0 010-1.414l6-6a1 1 0 011.414 0l6 6a1 1 0 01-1.414 1.414L11 5.414V17a1 1 0 11-2 0V5.414L4.707 9.707a1 1 0 01-1.414 0z" clip-rule="evenodd" />
    </svg>
</button>

For scrolling to the element and element being at the top of the page用于滚动到页面顶部的元素和元素

WebElement tempElement=driver.findElement(By.cssSelector("input[value='Excel']"));

            ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", tempElement);

A simple example of scroll to (using html is much more efficient but here is how to do it with JavaScript):滚动到的一个简单示例(使用 html 效率更高,但这里是如何使用 JavaScript 执行此操作):

 const btn = document.querySelector('.btn'); btn.addEventListener('click',()=>{ window.scrollTo({ left: 0, top: 0, })}) window.addEventListener('scroll', function() { const scrollHeight = window.pageYOffset; if (scrollHeight > 500) { btn.classList.add('show-link'); } else { btn.classList.remove('show-link'); } });
 .section { padding-bottom: 5rem; height: 90vh; } .btn { position: fixed; bottom: 3rem; right: 3rem; background: blue; width: 2rem; height: 2rem; color: #fff; visibility: hidden; z-index: -100; } .show-link { visibility: visible; z-index: 100; } .title h2 { text-align: center; }
 <section class="section"> <div class="title"> <h2>Section One</h2> </div> </section> <section class="section"> <div class="title"> <h2>Section Two</h2> </div> </section> <section class="section"> <div class="title"> <h2>Section Three</h2> </div> </section> <a class="btn"> </a>

Please check the below code, surely it will be helpful.请检查下面的代码,肯定会有帮助。 :) :)

document.querySelector('.sample-modal .popup-cta').scrollIntoView(true);
document.querySelector('.sample-modal').style.scrollPadding = '50px'; //to move to the top when scrolled up.

Try this solution using simple css just put css scroll-behavior:smooth on html and body as i have applied in css.使用简单的 css 尝试此解决方案只需将 css scroll-behavior:smooth放在html and body上,就像我在 css 中应用的那样。 that's it而已

 document.querySelector('a').onclick = function() { window.scrollTo(0,0) }
 html,body{ scroll-behavior: smooth; } a{ background-color: red; color:#fff; border-radius: 10px; margin:10px 0; display: inline-block; padding:10px 20px; }
 <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <a>Scroll to Top</a>

I Hope this will help you a lot.我希望这会对你有很大帮助。 Please do let me know your thoughts请让我知道你的想法

Funnily enough, most of these did not work for me AT ALL, so I used jQuery-ScrollTo.js with this:有趣的是,其中大部分对我来说根本不起作用,所以我使用了 jQuery-ScrollTo.js:

wrapper.find(".jumpToTop").click(function() {
    $('#wrapper').ScrollTo({
        duration: 0,
        offsetTop: -1*$('#container').offset().top
    });
});

And it worked.它奏效了。 $(document).scrollTop() was returning 0 , and this one actually worked instead. $(document).scrollTop()返回0 ,而这个实际上起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM