簡體   English   中英

jQuery scrollTop無法與CSS視差一起使用?

[英]jQuery scrollTop not working with CSS parallax?

我一直在制作一個使用視差的網站,該網站與http://keithclark.co.uk/articles/pure-css-parallax-websites/demo3/中的使用方式非常相似,並且我一直在嘗試實現一些jquery這將使頁面在頁面加載時自動滾動到底部。 但是,我正在使用的命令根本不會移動滾動條。 我曾在靜態網站上嘗試過此方法,但似乎工作正常。

$(document).ready(function() {
  $("html, body").animate({
      scrollTop: $(document).height()
    }, 2000);
  return false;
});

我的代碼(摘錄自文本)如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link href='https://fonts.googleapis.com/css?family=Arvo' rel='stylesheet' type='text/css'>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style>
  .email a {
    color: #001F67;
    text-decoration: none;
  }

  a {
    text-decoration: none;
  }

  .title {
    text-align: center;
    position: absolute;
    left: 50%;
    top: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    color: Black;
    font-size: 6vmin;
  }

  .About {
    text-align: center;
    position: absolute;
    left: 50%;
    top: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    color: #001F67;
    text-decoration: none;
    font-size: 4vmin;
    background-color: white;
  }

  .parallax {
    height: 500px; 
    height: 100vh;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-perspective: 300px;
    perspective: 300px;
  }

  .parallax__group {
    position: relative;
    height: 500px; 
    height: 100vh;
    -webkit-transform-style: preserve-3d;
    transform-style: preserve-3d;
  }

  .parallax__layer {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }

  .parallax__layer--fore {
    -webkit-transform: translateZ(90px) scale(.7);
    transform: translateZ(90px) scale(.7);
    z-index: 1;
  }

  .parallax__layer--base {
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    z-index: 4;
  }

  .parallax__layer--back {
    -webkit-transform: translateZ(-300px) scale(2);
    transform: translateZ(-550px) scale(3);
    z-index: 3;
  }

  .parallax__group {
    -webkit-transition: -webkit-transform 0.5s;
    transition: transform 0.5s;
  }

  body, html {
    overflow: hidden;
  }

  body {
    font: 100% / 1.5 Arvo;
  }

  * {
    margin:0;
    padding:0;
  }

  .parallax {
    font-size: 200%;
  }

  #group2 {
    z-index: 3;
  }

  #group2 .parallax__layer--back {
    background: #FFFFFF;
  }

  #group3 {
    z-index: 4;
  }

  #group3 .parallax__layer--base {
    /*background: black;*/
  }
</style>
<script>
$(document).ready(function() {
  $("html, body").animate({
      scrollTop: $(document).height()
    }, 2000);
  return false;
});
</script>
</head>
<body>
  <div class="parallax">
    <div id="group2" class="parallax__group">
      <div class="parallax__layer parallax__layer--base">
      </div>
      <div class="parallax__layer parallax__layer--back">
        <div class="title"><strong>Example Text</strong></div>
      </div>
    </div>
    <div id="group3" class="parallax__group">
      <div class="parallax__layer parallax__layer--fore">
        <div class="About">
          <!-- design inspired by graham hicks -->
          <p>
            Example Text
          <p>
            Example Text
          </p>
          <p class = "email">
            <br /> Example Text 
          </p>
        </div>
      </div>
      <div class="parallax__layer parallax__layer--base">
      </div>
    </div>
  </div>
</body>
</html>

有誰知道為什么它不起作用?

$("html, body")更改$("html, body") $(".parallax")以使其正常工作。

$(document).ready(function() {
  $(".parallax").animate({
      scrollTop: $(document).height()
    }, 2000);
  return false;
});

JSFiddle: https ://jsfiddle.net/qs0vbbtx/

暫無
暫無

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

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