移动设备和平板电脑上的奇怪滚动行为

huangapple go评论52阅读模式
英文:

Weird scrolling behavior on mobile and tablet

问题

I made a website and thought it would be cool with a parallax effect for my hero. So I implemented one, and on desktop it works just fine. But when I switch it over to mobile or tablet the scrolling gets weird. When I try to scroll upwards sometimes it works just fine and sometimes it just doesn't scroll at all. I don't know if the problem even has to do with the parallax effect but it would be my best guess.

I'll link a snippet with all my code down below, but what I want some help with understanding is why the scrolling is as it is on mobile and tablet, and how I can fix it or just helping me understand how to fix it.

FYI I also have a "scroll to top" feature with a button that appears when scrolling down and scrolls to the top when clicked. But sometimes when I tried implementing the parallax it stopped working as I expect it to. Sometimes it appears and sometimes it just doesn't. And when it does appear and I click it, I get some double scrollbar stuff that I think could have to do with the problem. (This is also just for mobile and tablet, on desktop it never appears)

英文:

I made a website and thought it would be cool with a parallax effect for my hero. So I implemented one, and on desktop it works just fine. But when I switch it over to mobile or tablet the scrolling gets wierd. When I try to scroll upwards sometimes it works just fine and sometimes it just doesn't scroll at all. I don't know if the problem even has to do with the parallax effect but it would be my best guess.

I'll link a snippet with all my code down below, but what I want some help with understanding is why the scrolling is as it is on mobile and tablet, and how I can fix it or just helping me understand how to fix it.

FYI I also have a "scroll to top" feature with a button that apears when scrolling down and scrolls to the top when clicked. But sometime when I tried implementing the parallax it stopped working as I expect it to. Sometimes it apears and sometimes it just doesn't. And when it do apear and I click it I get some double scrollbar stuff that I think could have to do with the problem. (This is also just for mobile and tablet, on desktop it never apears)

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

$(document).ready(function () {
  // Hamburger
  $(&quot;.burger-container&quot;).click(function () {
    $(&quot;.nav-menu&quot;).toggleClass(&quot;menu-opened&quot;);
    $(&quot;body&quot;).toggleClass(&quot;unscrollable&quot;);
    $(&quot;.toggler-container&quot;).slideToggle(&quot;slow&quot;);
  });

  // Dark-Light switcher
  $(&quot;body&quot;).toggleClass(&quot;light&quot;, localStorage.toggled == &quot;light&quot;);

  document.getElementById(&quot;checkBox&quot;).addEventListener(&quot;click&quot;, darkLight);

  function darkLight() {
    if (localStorage.toggled != &quot;light&quot;) {
      $(&quot;body&quot;).toggleClass(&quot;light&quot;, true);
      localStorage.toggled = &quot;light&quot;;
    } else {
      $(&quot;body&quot;).toggleClass(&quot;light&quot;, false);
      localStorage.toggled = &quot;&quot;;
    }
  }

  if ($(&quot;body&quot;).hasClass(&quot;light&quot;)) {
    $(&quot;#checkBox&quot;).prop(&quot;checked&quot;, true);
  } else {
    $(&quot;#checkBox&quot;).prop(&quot;checked&quot;, false);
  }
});

// Scroll to top
button = document.getElementById(&quot;scroll-button&quot;);

button.addEventListener(&quot;click&quot;, topFunction);

window.onscroll = function () {
  scrollFunction();
};

function scrollFunction() {
  if (document.body.scrollTop &gt; 20 || document.documentElement.scrollTop &gt; 20) {
    button.style.display = &quot;block&quot;;
  } else {
    button.style.display = &quot;none&quot;;
  }
}

function topFunction() {
  window.scrollTo({
    top: 0,
    left: 0,
    behavior: &quot;smooth&quot;,
  });
}

<!-- language: lang-css -->

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Dark colors */
:root {
--white-color: #d9d9d9;
--black-color: #0d0d0d;
--grey-color-1: #595959;
--body-bg-color: #262626;
--text-hover-color: #a6a6a6;
}
/* Light colors */
:root .light {
--body-bg-color: #dfdfdf;
--black-color: blue;
--white-color: black;
}
/* ======== MAIN CSS ======== */
html {
width: 100%;
height: 100%;
}
body {
width: 100%;
height: 100%;
background-color: var(--body-bg-color) !important;
position: relative;
overflow: hidden;
font-family: &quot;Nunito&quot;, sans-serif;
color: var(--white-color);
}
main {
background-color: var(--body-bg-color);
}
.wrapper {
height: 100vh;
overflow-y: auto;
overflow-x: hidden;
perspective: 10px;
}
header {
z-index: 999;
position: sticky;
top: 0;
left: 0;
width: 100%;
transition: 0.5s ease;
}
nav {
width: 100vw;
height: 3.5rem;
background-color: var(--black-color);
}
.unscrollable {
overflow: hidden;
}
.active {
text-decoration: underline;
}
/* ======= PARALLAX ======= */
.parallax {
position: relative;
display: flex;
justify-content: center;
align-items: center;
height: 100%;
transform-style: preserve-3d;
z-index: -1;
}
.background {
transform: translateZ(-10px) scale(2);
-webkit-user-drag: none;
user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
.foreground {
transform: translateZ(-5px) scale(1.5);
-webkit-user-drag: none;
user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
.background,
.foreground {
position: absolute;
height: 100%;
width: 100%;
object-fit: cover;
z-index: -1;
}
.hero-text {
text-align: center;
color: var(--white-color);
position: absolute;
top: 50%;
left: 50%;
min-width: 14rem;
transform: translate(-50%, -50%);
}
.hero-text &gt; h1 {
font-size: 3rem;
}
.hero-text &gt; p {
font-size: 1rem;
}
/* ======== HAMBURGER ======== */
.burger-container {
position: absolute;
right: 0.5rem;
top: 0.1rem;
display: inline-block;
height: 50px;
width: 50px;
cursor: pointer;
transform: rotate(0deg);
transition: all 0.3s cubic-bezier(0.4, 0.01, 0.165, 0.99);
user-select: none;
z-index: 999;
-webkit-tap-highlight-color: transparent;
}
#burger {
position: relative;
top: 50%;
width: 18px;
height: 8px;
display: block;
margin: -4px auto 0;
}
.bar {
position: relative;
width: 100%;
height: 1px;
display: block;
background: var(--white-color);
transition: all 0.3s cubic-bezier(0.4, 0.01, 0.165, 0.99);
transition-delay: 0s;
}
.topBar {
transform: translateY(0px) rotate(0deg);
}
.btmBar {
transform: translateY(6px) rotate(0deg);
}
.menu {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
padding: 0;
height: 100%;
list-style: none;
}
.nav-menu {
transition: all 0.3s ease;
}
.menu-item {
transition: transform 0.5s cubic-bezier(0.4, 0.01, 0.165, 0.99),
opacity 0.6s cubic-bezier(0.4, 0.01, 0.165, 0.99);
border-bottom: var(--body-bg-color) solid 1px;
opacity: 0;
}
.menu-item a {
display: block;
position: relative;
color: var(--white-color);
font-family: &quot;Roboto&quot;, sans-serif;
font-weight: 300;
text-decoration: none;
font-size: 1.4rem;
line-height: 2.35;
width: 100%;
}
.menu-item a:hover {
color: var(--text-hover-color);
}
.nav-menu.menu-opened {
position: absolute;
top: 0;
height: 100vh;
width: 100vw;
background-color: var(--black-color);
opacity: 0.92;
backdrop-filter: blur(5px);
box-shadow: inset 0 0 5rem #000000;
transition: all 0.3s ease-in, 0.5s ease-in;
transition-delay: 0.1s;
}
.nav-menu.menu-opened .burger-container {
transform: rotate(90deg);
}
.nav-menu.menu-opened .burger-container #burger .bar {
transition: all 0.4s cubic-bezier(0.4, 0.01, 0.165, 0.99);
transition-delay: 0.2s;
}
.nav-menu.menu-opened .burger-container #burger .bar.topBar {
transform: translateY(4px) rotate(45deg);
}
.nav-menu.menu-opened .burger-container #burger .bar.btmBar {
transform: translateY(3px) rotate(-45deg);
}
.nav-menu.menu-opened ul.menu li.menu-item {
transform: scale(1) translateY(0px);
opacity: 1;
}
/* ======== Dark Light switcher ====== */
.switcher &gt; span {
color: var(--white-color);
padding: 0 0.5rem;
font-size: 1.3rem;
}
.switcher {
display: flex;
align-items: center;
}
.toggler-container {
display: none;
margin-top: 1rem;
}
.switch {
position: relative;
align-self: center;
display: inline-block;
width: 3.2rem;
height: 1.5rem;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
cursor: pointer;
background-color: #808080;
-webkit-transition: 0.4s;
transition: 0.4s;
border-radius: 1rem;
}
.slider:before {
position: absolute;
left: 4px;
bottom: 4px;
content: &quot;&quot;;
height: 1rem;
width: 1rem;
background-color: var(--white-color);
-webkit-transition: 0.4s;
transition: 0.4s;
border-radius: 50%;
}
input:checked + .slider {
background-color: #808080;
}
input:checked + .slider:before {
-webkit-transform: translateX(1.7rem);
-ms-transform: translateX(1.7rem);
transform: translateX(1.7rem);
}
/* ======== CONTENT ======= */
.content {
text-align: center;
margin: 0 5% 0 5%;
padding-top: 1.5rem;
color: var(--white-color);
}
.content h2 {
font-size: 1.4rem;
font-family: &quot;Roboto&quot;, sans-serif;
}
.content p {
font-size: 1.1rem;
font-family: &quot;Nunito&quot;, sans-serif;
}
.img {
display: block;
}
/* ======== UP ARROW ========*/
#scroll-button {
display: none;
position: fixed;
bottom: 1rem;
right: 1.2rem;
z-index: 998;
border: none;
outline: none;
background-color: transparent;
color: var(--white-color);
font-size: 1.5rem;
cursor: pointer;
border-radius: 50%;
}
/* ======== FOOTER ======== */
footer {
padding: 3rem 0 6rem 0;
background-color: var(--grey-color-1);
position: sticky;
bottom: 0;
z-index: -2;
}
footer ul {
margin: 0;
padding: 0;
list-style: none;
text-align: center;
font-size: 1.3rem;
}
footer ul a {
color: var(--white-color);
text-decoration: none;
}
footer ul li {
display: block;
padding: 0.25rem 0;
}
footer ul a:hover {
color: var(--text-hover-color);
}
footer hr {
color: var(--text-hover-color);
max-width: 13rem;
margin: 1.5rem auto;
}
footer .copyright {
margin-top: 1rem;
text-align: center;
font-size: 0.75rem;
color: var(--text-hover-color);
}
/* ======== MEDIA QUERIES ======== */
/* TABLET */
@media only screen and (min-width: 600px) {
/* ======= FOOTER ======= */
footer ul {
font-size: 1.1rem;
}
footer ul li {
display: inline-block;
padding: 0 0.2rem;
}
footer hr {
max-width: 35rem;
}
}
/* DESKTOP */
@media only screen and (min-width: 769px) {
/* ======== MAIN CSS ======== */
.hero-text {
position: absolute;
top: 40%;
left: 30%;
max-width: 20rem;
}
.hero-text &gt; h1 {
font-size: 3.5rem;
}
.hero-text &gt; p {
font-size: 1.2rem;
}
/* ======= Scrollbar ======== */
::-webkit-scrollbar {
width: 7px;
background: transparent;
}
::-webkit-scrollbar-thumb {
border-radius: 100px;
background: rgb(109, 109, 109);
}
::-webkit-scrollbar-thumb:hover {
background-color: rgb(148, 148, 148);
}
/* ======== HAMBURGER ======== */
nav {
width: 100%;
background-color: var(--black-color);
backdrop-filter: none;
}
.nav-menu {
transition: all 0.3s ease-in, 0.5s ease-in;
transition-delay: 0.25s;
width: 100%;
height: 3.5rem;
display: flex;
align-items: center;
}
.nav-menu.menu-opened {
height: 100%;
}
.burger-container {
display: none;
}
.menu-item {
transform: scale(1) translateY(0px);
opacity: 1;
display: inline;
margin-right: 0.8rem;
border-bottom: 0;
margin-top: 0;
}
.menu {
flex-direction: row;
position: absolute;
top: 0;
margin: 0 0 0 1.5rem;
height: 3.5rem;
width: 100%;
}
.menu-item a {
display: inline;
font-size: 1rem;
transition: all 0.15s cubic-bezier(0.54, 0.12, 0.54, 0.94);
}
.flex-filler {
flex: 1 1 auto;
}
/* ======== Dark-light switcher ======== */
.toggler-container {
display: block;
margin: 0 2.5rem 0 0;
}
.switcher &gt; span {
font-size: 1.2rem;
padding: 0 0.4rem;
}
.switch {
width: 2.5rem;
height: 1.3rem;
}
.slider:before {
height: 0.8rem;
width: 0.8rem;
left: 4px;
bottom: 4px;
}
input:checked + .slider:before {
-webkit-transform: translateX(1.2rem);
-ms-transform: translateX(1.2rem);
transform: translateX(1.2rem);
}
/* ======== CONTENT ========*/
.content {
margin: 0 20% 0 20%;
padding-top: 2rem;
}
.content h2 {
font-size: 1.5rem;
}
.content p {
font-size: 1.15rem;
}
/* ======== UP ARROW ========*/
#scroll-button {
position: fixed;
height: 3rem;
width: 3rem;
bottom: 1.1rem;
right: 1.1rem;
}
#scroll-button:hover {
background-color: rgba(160, 160, 160, 0.568);
backdrop-filter: blur(15px);
}
/* ======= FOOTER ======= */
}
/* XL-DESKTOP */
@media only screen and (min-width: 1200px) {
/* ======= HAMBURGER ======= */
.menu-item {
margin-right: 1.3rem;
}
.menu-item a {
font-size: 1.1rem;
}
/* ======= FOOTER ======= */
footer ul {
font-size: 1.2rem;
}
footer ul li {
padding: 0 0.4rem;
}
footer hr {
max-width: 40rem;
}
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;sv&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot; /&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot; /&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;
&lt;title&gt;Lagar och s&#228;kerhet | Hem&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;css/style.css&quot; /&gt;
&lt;link
href=&quot;https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&amp;display=swap&quot;
rel=&quot;stylesheet&quot;
/&gt;
&lt;link
href=&quot;https://fonts.googleapis.com/css2?family=Nunito:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;0,1000;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900;1,1000&amp;display=swap&quot;
rel=&quot;stylesheet&quot;
/&gt;
&lt;link
href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot;
rel=&quot;stylesheet&quot;
/&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;link
rel=&quot;stylesheet&quot;
href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css&quot;
/&gt;
&lt;link
rel=&quot;stylesheet&quot;
href=&quot;https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0&quot;
/&gt;
&lt;script defer src=&quot;js/script.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;!--
Att g&#246;ra: 
Fixa bilder till Hero parallax
Skriva content
Fixa bilder till content (Picture element, bootstrap)
L&#228;gga in all content p&#229; index och subpages (Bootstrap)
Fixa up arrow s&#229; den funkar
Tablet responsive media query
Hamburger close animation (Inte prio)
--&gt;
&lt;body&gt;
&lt;header&gt;
&lt;nav&gt;
&lt;div class=&quot;nav-menu&quot;&gt;
&lt;div class=&quot;burger-container&quot;&gt;
&lt;div id=&quot;burger&quot;&gt;
&lt;div class=&quot;bar topBar&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;bar btmBar&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;ul class=&quot;menu&quot;&gt;
&lt;li class=&quot;menu-item&quot;&gt;&lt;a href=&quot;#&quot;&gt;Hem&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;menu-item&quot;&gt;&lt;a href=&quot;html/losenord.html&quot;&gt;L&#246;senord&lt;/a&gt;&lt;/li&gt;
&lt;li class=&quot;menu-item&quot;&gt;
&lt;a href=&quot;html/sakerhetshot.html&quot;&gt;S&#228;kerhetshot&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;menu-item&quot;&gt;
&lt;a href=&quot;html/statlig-kontroll.html&quot;&gt;Statlig kontroll&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;menu-item&quot;&gt;
&lt;a href=&quot;html/tredjepartskod.html&quot;&gt;Tredjepartskod&lt;/a&gt;
&lt;/li&gt;
&lt;li class=&quot;menu-item&quot;&gt;
&lt;a href=&quot;html/webbhotell.html&quot;&gt;Webbhotell&lt;/a&gt;
&lt;/li&gt;
&lt;div class=&quot;flex-filler&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;toggler-container&quot;&gt;
&lt;div class=&quot;switcher&quot;&gt;
&lt;span class=&quot;material-symbols-outlined&quot;&gt; dark_mode &lt;/span&gt;
&lt;label class=&quot;switch&quot;&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;checkBox&quot; /&gt;
&lt;span class=&quot;slider&quot;&gt;&lt;/span&gt;
&lt;/label&gt;
&lt;span class=&quot;material-symbols-outlined&quot;&gt; light_mode &lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/nav&gt;
&lt;/header&gt;
&lt;div class=&quot;wrapper&quot;&gt;
&lt;header class=&quot;parallax&quot;&gt;
&lt;img src=&quot;https://cdn.imgpaste.net/2022/10/10/Kem93m.png&quot; class=&quot;background&quot; /&gt;
&lt;img src=&quot;https://cdn.imgpaste.net/2022/10/10/KemO9N.png&quot; class=&quot;foreground&quot; /&gt;
&lt;div class=&quot;hero-text&quot;&gt;
&lt;h1&gt;Lagar och s&#228;kerhet&lt;/h1&gt;
&lt;p&gt;
Hemsidan om lagar och s&#228;kerhet p&#229; internet h&#228;r st&#229;r det massa saker wow omg coolt :I
&lt;/p&gt;
&lt;/div&gt;
&lt;/header&gt;
&lt;main&gt;
&lt;div class=&quot;content&quot;&gt;
&lt;section&gt;
&lt;h2&gt;Hejehje&lt;/h2&gt;
&lt;p&gt;
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Magni in aut quisquam ipsa,
reiciendis quidem rem facilis quaerat nam est! Lorem ipsum dolor sit amet consectetur,
adipisicing elit. Libero, nulla perferendis? Officia illo vitae alias obcaecati nulla
porro quisquam, praesentium id vel ex culpa deleniti neque reiciendis officiis nisi
quaerat.
&lt;/p&gt;
&lt;/section&gt;
&lt;img class=&quot;img&quot;            src=&quot;https://ichef.bbci.co.uk/news/999/cpsprodpb/15951/production/_117310488_16.jpg&quot; alt=&quot;picture&quot;&gt;
&lt;/div&gt;
&lt;button id=&quot;scroll-button&quot; title=&quot;G&#229; till toppen&quot;&gt;
&lt;i class=&quot;fas fa-arrow-up&quot;&gt;&lt;/i&gt;
&lt;/button&gt;
&lt;/main&gt;
&lt;footer&gt;
&lt;ul class=&quot;list&quot;&gt;
&lt;li&gt;
&lt;a href=&quot;#&quot; class=&quot;active&quot;&gt;Hem&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;html/losenord.html&quot;&gt;L&#246;senord&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;html/sakerhetshot.html&quot;&gt;S&#228;kerhetshot&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;html/statlig-kontroll.html&quot;&gt;Statlig kontroll&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;html/tredjepartskod.html&quot;&gt;Tredjepartskod&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&quot;html/webbhotell.html&quot;&gt;Webbhotell&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;p class=&quot;copyright&quot;&gt;Copyright &amp;copy; All rights reserved by Vincent Cornelius&lt;/p&gt;
&lt;/footer&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

你想滚动具有包装器类的 div,但由于您的 html 标签具有比视口更高的高度,您首先滚动它,这会导致您正在经历的奇怪行为。这是由具有菜单类的 ul 元素引起的。您可以通过向您的 header 或 html 标签添加 overflow:hidden,或者通过从您的 menu 元素中移除 position: absolute 来避免这种情况。

英文:

Well you want to scroll the div with wrapper class but since your html tag has a higher height than the viewport you are scrolling it first which makes the odd behavior you are experiencing. This is caused by ul element with menu class. You can avoid this by adding overflow:hidden to your header or html tag or by removing the position: absolute from your menu element.

huangapple
  • 本文由 发表于 2023年5月10日 19:20:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定