簡體   English   中英

javascript:onclick=“scrollWin()”沒有按預期工作

[英]javascript: onclick=“scrollWin()” doesn't work as intended

我有以下簡單頁面:

  • 一個 header 左上角的名稱在您向下滾動時會消失(您會看到右下角的菜單)
  • 固定的背景圖片(視差)
  • 和右下角的菜單,向下滾動時會顯示(因為視差)

除了用鼠標滾動之外,我希望當您單擊頂部的名稱時,頁面應該自動向下滾動到頁面底部並顯示菜單。 問題是我的 onclick="scrollWin()" function 頁面向下滾動一毫秒並出現菜單,但隨后頁面自行向上滾動一點,菜單再次消失,而當頁面有被滾動到底部。 我認為問題可能出在視差效應上。 請問你能幫幫我嗎?

這是代碼:


<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="UTF-8">
    <style>
        body,
        html {
            height: 100%;
            margin: 0;
            font-family: 'Karla';
            font-size: 22px;

        }


        * {
            box-sizing: border-box;
        }

        .header {
            margin-left: 190px;
            margin-top: 40px;
            text-align: left;
            opacity: 0.4;

        }

        .container {
            height: 10vh;
            background-color: white;
        }

        .container1 {
            height: 10vh;
            background-color: white;
        }

        .textabout {

            position: relative;
            top: 200px;
            left: 900px;
            text-align: center;
            opacity: 0.5;
            text-align: justify;
            text-justify: inter-word;
            /* scroll-snap-align: start;*/


        }

        .parallax {
            /* The image used */
            background-image: url("IMG_7916_cut.jpg");
            opacity: 0.8;


            /* Full height */
            height: 100%;

            /* Create the parallax scrolling effect */
            background-attachment: fixed;
            background-position: center;
            background-repeat: no-repeat;
            background-size: 567px 756px;
        }

        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: white;
            height: 10vh;
            margin-right: 30px;
            margin-bottom: 30px;
        }

        li {
            float: right;
        }

        li a {
            display: block;
            color: black;
            text-align: center;
            text-decoration: none;
            opacity: 0.4;
            padding: 16px 16px;
            margin-left: 30px;
            margin-right: 30px;
            transition: 0.2s;
        }

        li a:hover,
        li a:focus {
            color: black;
            opacity: 1;
        }

        /* On screens that are 992px wide or less */
        @media screen and (max-width: 600px) {

            .header {
                position: relative;
                text-align: left;
                display: block;
                margin-left: 80.5px;
                margin-right: auto;
            }

            ul {
                margin-right: 17px;
            }

            .parallax {
                background-size: 85%;
            }
        }

    </style>
</head>

<body>
    <div class="container1">
        <div class="header">
            <a href="" onclick="scrollWin()">|bacheva</a>
        </div>
    </div>
    <div class="parallax"></div>

    <div class="container">

        <ul style="font-size:22px;">
            <li><a href="about_final.html">|about</a></li>
            <li><a href="project%20page_final.html">|work</a></li>
            <li><a href="home_final.html">|home</a></li>
        </ul>
    </div>


    <script>
        function scrollWin() {
                        window.scrollTo(0, document.body.scrollHeight); 
        }

    </script>

</body>

</html>

預先感謝您的幫助。

yes, you can change it to be a div (notice i gave the div class="btn" and on the css i added cursor: pointer you can see working example here: https://codepen.io/Elnatan/pen/JjRvpaJ

<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="UTF-8">
    <style>
        body,
        html {
            height: 100%;
            margin: 0;
            font-family: 'Karla';
            font-size: 22px;

        }


        * {
            box-sizing: border-box;
        }

        .header {
            margin-left: 190px;
            margin-top: 40px;
            text-align: left;
            opacity: 0.4;

        }

        .container {
            height: 10vh;
            background-color: white;
        }

        .container1 {
            height: 10vh;
            background-color: white;
        }
      .btn{
        cursor:pointer;
      }
        .textabout {

            position: relative;
            top: 200px;
            left: 900px;
            text-align: center;
            opacity: 0.5;
            text-align: justify;
            text-justify: inter-word;
            /* scroll-snap-align: start;*/


        }

        .parallax {
            /* The image used */
            background-image: url("IMG_7916_cut.jpg");
            opacity: 0.8;


            /* Full height */
            height: 100%;

            /* Create the parallax scrolling effect */
            background-attachment: fixed;
            background-position: center;
            background-repeat: no-repeat;
            background-size: 567px 756px;
        }

        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: white;
            height: 10vh;
            margin-right: 30px;
            margin-bottom: 30px;
        }

        li {
            float: right;
        }

        li a {
            display: block;
            color: black;
            text-align: center;
            text-decoration: none;
            opacity: 0.4;
            padding: 16px 16px;
            margin-left: 30px;
            margin-right: 30px;
            transition: 0.2s;
        }

        li a:hover,
        li a:focus {
            color: black;
            opacity: 1;
        }

        /* On screens that are 992px wide or less */
        @media screen and (max-width: 600px) {

            .header {
                position: relative;
                text-align: left;
                display: block;
                margin-left: 80.5px;
                margin-right: auto;
            }

            ul {
                margin-right: 17px;
            }

            .parallax {
                background-size: 85%;
            }
        }

    </style>
</head>

<body>
    <div class="container1">
        <div class="header">
            <div class="btn" onclick="scrollWin()">|bacheva</div>
        </div>
    </div>
    <div class="parallax"></div>

    <div class="container">

        <ul style="font-size:22px;">
            <li><a href="about_final.html">|about</a></li>
            <li><a href="project%20page_final.html">|work</a></li>
            <li><a href="home_final.html">|home</a></li>
        </ul>
    </div>


    <script>
        function scrollWin() {
                        window.scrollTo(0, document.body.scrollHeight); 
        }

    </script>

</body>

</html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM