简体   繁体   English

如何使这种视差效果在移动浏览器上起作用?

[英]How can I make this parallax effect work on mobile browsers?

I'm trying to create a parallax effect based on this codepen: https://codepen.io/chaobu/pen/qsyhf 我正在尝试基于此Codepen创建视差效果: https ://codepen.io/chaobu/pen/qsyhf

However, on mobile, this does not work. 但是,在移动设备上,这不起作用。 How can I have the parallax effect work on mobile browsers? 如何使视差效果在移动浏览器上起作用?

It seems that the Javascript here does not affect the parallax effect. 看来这里的Javascript不会影响视差效果。 I initially thought to change the javascript click event to touch..., however that did not work. 最初,我想将javascript click事件更改为touch ...,但这没有用。 I also tried creating a media query for the background images and position: sticky; 我还尝试为背景图片和位置创建媒体查询:粘性; them, and that did not work either. 他们,那也不起作用。

HTML: HTML:

<section id="1">
                    <article>
                      <p class="title"><strong>OUR WORK</strong></p>
                      <a class="homelink" href="#"><p class="link">FULL PROJECT LIST</p></a>
                    </article>
                  </section>
                  <section id="2">
                    <article>
                      <p class="title"><strong>DRAWINGS</strong></p>
                      <a class="homelink" href="#"><p>CONTENT LINK</p></a>
                    </article>
                  </section>
                  <section id="3">
                    <article>
                      <p class="title"><strong>WHO WE ARE:</strong></p>
                      <a class="homelink" href="#"><p>CONTENT LINK</p></a>
                    </article>
                  </section>
                  <section id="4">
                    <article>
                      <p class="title"><strong>WHAT WE DO:</strong></p>
                      <a class="homelink" href="#"><p>CONTENT LINK</p></a>
                    </article>
                  </section>
                  <section id="5">
                    <article>
                      <h1>TITLE</h1>
                      <a class="homelink" href="#"><p>CONTENT LINK</p></a>
                    </article>
                  </section>
                </div>

CSS: CSS:

.wrapper {
  position: relative;
  box-shadow: 0 0 1em #333333;
}
.wrapper section {
  position: relative;
  background: #00477C;
}
.wrapper section article {
  width: 50%;
  margin: 0 auto;
  padding: 2em 0;
}
.wrapper section article p {
  margin-bottom: 1em;
}
.wrapper section article p:last-of-type {
  margin-bottom: 0;
}
.wrapper section:after {
  content: "";
  display: block;
  position: relative;
  background-attachment: fixed;
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  height: 100vh;
  width: 100%;
}

.img-src {
    position: fixed;
    background-position: center;
    -webkit-background-size: cover;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: -1;
}
.wrapper section:nth-child(1):after {
  background-image: url("../img/test.jpg");
}
.wrapper section:nth-child(2):after {
  background-image: url("../img/test2.jpg");
}
.wrapper section:nth-child(3):after {
  background-image: url("../img/test3.jpg");
}
.wrapper section:nth-child(4):after {
  background-image: url("../img/test4.jpg");
}
.wrapper section:nth-child(5):after {
  background-image: url("../img/test.jpg");
}
.wrapper section:nth-child(6):after {
  background-image: url("../img/test2.jpg");
}

@media only screen and (max-width: 600px) {
  .wrapper section:nth-child(1):after {
    position: -webkit-sticky;
    top:65px;
    background-image: url("../img/test.jpg");
  }
  .wrapper section:nth-child(2):after {
    position: -webkit-sticky;
    background-image: url("../img/test2.jpg");
  }
  .wrapper section:nth-child(3):after {
    position: -webkit-sticky;
    background-image: url("../img/test3.jpg");
  }
  .wrapper section:nth-child(4):after {
    position: -webkit-sticky;
    background-image: url("../img/test4.jpg");
  }
  .wrapper section:nth-child(5):after {
    position: -webkit-sticky;
    background-image: url("../img/test.jpg");
  }
  .wrapper section:nth-child(6):after {
    position: -webkit-sticky;
    background-image: url("../img/test2.jpg");
  }
}

@media only screen and (max-width: 600px) {
  .wrapper section article {
    width: 80%;
  }
  .wrapper section:after {
    height: 100vh;
  }
}

I expected the parallax to work on mobile, however it does not. 我希望视差能在移动设备上工作,但事实并非如此。 Could anyone help out with this? 有人可以帮忙吗? Thanks! 谢谢!

Parallax scrolling works differently on mobile devices. 视差滚动在移动设备上的工作方式有所不同。 It may be slow, or not work at all. 它可能很慢,或者根本不起作用。 Android devices do it slowly, and iOS devices only do it once scrolling has stopped. Android设备运行缓慢,而iOS设备仅在滚动停止后才运行。 Read up here for more information. 此处阅读更多信息。

On a lot of mobile browsers, the parallax effect doesn't work as intended, or at all. 在许多移动浏览器上,视差效果并没有达到预期效果,或者根本没有效果。 There's a very basic demo on W3 which can be seen here . 在W3上有一个非常基本的演示,可以在这里看到。 Test it out in your mobile browser and the effect might fail as well. 在您的移动浏览器中进行测试,效果可能也会失败。 It certainly did on mine. 它肯定是对我的。

I do want to provide an alternative though. 我确实想提供一个替代方案。 I've had similar issues on my parallax effect, as well as on other desktop browsers. 我的视差效果以及其他桌面浏览器也遇到了类似的问题。 Instead of using background-attachment: fixed , which is killing browser performance and might very well be the reason why most mobile browsers don't interpret this as intended, I've used jQuery to translate the Y-axis of the background with the scroll value. 而不是使用background-attachment: fixed ,这会降低浏览器的性能,并且很可能是大多数移动浏览器无法按预期解释此问题的原因,我使用jQuery通过滚动来平移背景的Y轴值。 This keeps the image in place. 这样可以将图像固定在适当的位置。

You can find the implementation of my solution here . 您可以在此处找到我的解决方案的实现。 The original code is also included, so you might be able to tweak it to fit your needs. 原始代码也包括在内,因此您可以对其进行调整以满足您的需求。 Let me know if this was helpful in any way. 让我知道这是否对您有帮助。

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

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