js+css实现支持手机和键盘操作的水平横向滚动网页显示代码

代码语言:html

所属分类:加载滚动

代码描述:js+css实现支持手机和键盘操作的水平横向滚动网页显示代码

代码标签: js css 支持 手机 键盘 操作 水平 横向 滚动 网页

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  

  
  
  
<style>
*, *::before, *::after {
  box-sizing: border-box;
}

:root {
  --bg-color:AntiqueWhite;
  --text-color:black;
  --scroll-pct:0;
}

body {
  margin: 0;
  overflow: hidden;
  color: var(--text-color);
  background-color: var(--bg-color);
  transition: background-color 1s, color 1s;
}

.h-track {
  width: 100%;
  height: 100vh;
  overflow-x: auto;
  position: relative;
  overflow-y: hidden;
}
.h-track .h-content-container {
  position: relative;
  max-width: unset;
  width: fit-content;
  height: 100%;
  display: flex;
  align-items: stretch;
  transition: transform 0.2s linear;
}
.h-track .h-content-container > * {
  display: grid;
  place-items: Center;
  min-width: 100vw;
  height: 100%;
}

.invisible-scrollbar {
  scrollbar-width: none;
}

.invisible-scrollbar::-webkit-scrollbar {
  display: none;
}

.progress-bar {
  margin: 10px auto;
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translate(-50%, 0);
  width: 100%;
  max-width: 50%;
  height: 10px;
  border-radius: 5px;
  border: 1px solid var(--text-color);
  overflow: hidden;
}
.progress-bar::before {
  content: "";
  display: block;
  background-color: var(--text-color);
  height: 100%;
  width: var(--scroll-pct);
}
</style>


  
</head>

<body >
  <div class="h-track">
  <div class="h-content-container">
    <header tabindex="1" data-bg-color="AntiqueWhite">
      <h1>Horizontal Scrolling Site</h1>
    </header>
    <section tabindex="1" data-bg-color="white">
      <p>Works with mouse wheel and trackpad on all axis...</p>
    </section>
    <section tabindex="1" data-bg-color="#222222" data-text-color="white">
      <p>Keyboard arrows and tabbing...</p>
    </section>
    <section tabindex="1" data-bg-color="coral">
      <p>and touch/drag on mobile.</p>
    </section>
    <section tabindex="1" data-bg-color="DarkSeaGreen" data-text-color="white">
      <h2>Thanks for playing!</h2>
    </section>
  </div>
  <div class="progress-bar"></div>
</div>
  
      <script id="rendered-js" >
const track = document.querySelector('.h-track');

if (track) {

  track.classList.add('invisible-scrollbar');
  const trackContainer = track.querySelector('.h-content-container');

  let registeredScroll = false;
  let outerWidth = track.offsetWidth;
  let innerWidth = trackContainer.scrollWidth;

  const updatePct = () => {
    const pct = track.scrollLeft / (innerWidth - outerWidth) * 100;

    track.dataset.scrollPercent = pct;
    document.documentElement.style.setProperty('--scroll-pct', `${track.dataset.scrollPercent}%`);
  };
  updatePct();

  window.addEventListener('resize', e => {
    outerWidth = track.offsetWidth;
    innerWidth = trackContainer.scrollWidth;
    updatePct();
  });

  track.addEventListener("scroll", evt => {
    if (!registeredScroll) {
      t.........完整代码请登录后点击上方下载按钮下载查看

网友评论0