簡體   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