繁体   English   中英

使用 JavaScript 滚动到页面顶部?

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

如何使用 JavaScript 滚动到页面顶部? 立即跳转到页面顶部的滚动条也是可取的,因为我不希望实现平滑滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

使用动画滚动到页面顶部

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

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

使用 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>

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

如何使用JavaScript滚动到页面顶部? 滚动条立即跳到页面顶部也是可取的,因为我不想获得平滑的滚动。

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;
    });
}

以上所有答案均不适用于SharePoint 2016。

它必须像这样完成: https : //sharepoint.stackexchange.com/questions/195870/

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

激活所有浏览器。 祝好运

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);
        }

尝试这个

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

如果您想滚动到任何带有 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();

如果您想为滚动动作设置动画,则无需 javascript , event !

CSS:

html {
    scroll-behavior: smooth;
}

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" });

当顶部滚动顶部小于限制底部和底部到顶部滚动标题是粘性的。 下面参见小提琴示例。

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/

只需尝试,无需其他插件/框架

 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; }

最短的

location='#'

此解决方案是对pollirrata 答案的改进,有一些缺点:无法平滑滚动和更改页面位置,但最短

使用 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>

用于滚动到页面顶部的元素和元素

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

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

滚动到的一个简单示例(使用 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>

请检查下面的代码,肯定会有帮助。 :)

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

使用简单的 css 尝试此解决方案只需将 css scroll-behavior:smooth放在html and body上,就像我在 css 中应用的那样。 而已

 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>

我希望这会对你有很大帮助。 请让我知道你的想法

有趣的是,其中大部分对我来说根本不起作用,所以我使用了 jQuery-ScrollTo.js:

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

它奏效了。 $(document).scrollTop()返回0 ,而这个实际上起作用了。

暂无
暂无

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

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