Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在vanilla/纯JS中无限滚动,不产生间隙

在vanilla/纯JS中无限滚动,不产生间隙
EN

Stack Overflow用户
提问于 2020-10-18 12:29:05
回答 2查看 256关注 0票数 1

当一个周期完成后,你就可以看到差距了。我想摆脱它。它将一个接一个地重新循环,而不会产生间隙。

谁能给我点时间来解决这个问题..

代码语言:javascript
运行
AI代码解释
复制
const scrollAnimation = (function() {
  let element = null;
  // let obejct = {};
  let scroller = true;
  let scrollSpeed = 0;
  let parentElement = null;
  let childElement = null;
  let viewHeight = 0;

  const toggleScrollEvent = function(e) {
    scroller = (e.type == 'mouseenter') ? false : true;
  }


  const setProperty = function(element, obejct) {

    viewHeight = document.querySelector(element).clientHeight

    scrollSpeed = obejct.scrollSpeed;

    // create child element 
    childElement = document.createElement('div');
    childElement.id = 'childElement';

    // append scroll content to  child element 
    let items = document.querySelectorAll(element + ' .item');
    items.forEach(function(item) {
      childElement.appendChild(item)
    });

    // append child element to parent element
    element = document.querySelector(element);
    parentElement = element.appendChild(childElement)

    // set child element top property value manually 200px
    // let hg = childElement.getBoundingClientRect();
    childElement.style.top = viewHeight + 'px';



    parentElement.addEventListener('mouseenter', toggleScrollEvent);
    parentElement.addEventListener('mouseleave', toggleScrollEvent);

  }

  const scrollEelement = function() {
    let posY = parseInt(childElement.style.top);
    if (scroller) {
      if (posY + childElement.clientHeight > 0) {
        childElement.style.top = posY - scrollSpeed + 'px'
      } else {
        childElement.style.top = viewHeight + 'px';
      }
    }
  }

  setInterval(scrollEelement, 50)



  return {
    init: function(element, obejct) {
      setProperty(element, obejct)
    }
  }

})([]);

window.addEventListener("DOMContentLoaded", function() {
  scrollAnimation.init('#parentElement', {
    scrollSpeed: 5,
  });
})
代码语言:javascript
运行
AI代码解释
复制
*,
*::after,
*::before {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body,
html {
  margin: 0;
  padding: 0;
}

#parentElement {
  position: relative;
  width: 400px;
  background: #eeeeee;
  height: 200px;
  overflow: hidden;
  border: 2px solid royalblue;
}

#parentElement #childElement {
  padding: 10px;
  position: absolute;
  width: 100%;
  top: 0;
  left: 0;
}
代码语言:javascript
运行
AI代码解释
复制
<div id="parentElement">
  <h1 class="item">Start</h1>
  <h1 class="item">Mid</h1>
  <h1 class="item">Mid</h1>
  <h1 class="item">Mid</h1>
  <h1 class="item">Mid</h1>
  <h1 class="item">Mid</h1>
  <h1 class="item">Mid</h1>
  <h1 class="item" style="background: orangered;">End</h1>
</div>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-18 12:55:21

在检查是否需要重新启动滚动的scrollEelement函数中,可以检查当前顶部位置是否小于某个".item“div上高度的负值。如果是,则从div中删除第一项,并将其再次追加到末尾。因为这些div的高度是45px,所以一旦childElement div的顶部小于-45,顶部的".item“div就不再可见,并且可以在div的末尾删除和替换-并调整childElement元素的顶部以添加回45px的已删除项。所以:

代码语言:javascript
运行
AI代码解释
复制
const scrollAnimation  = (function(){
      let element = null;
      // let obejct = {};
      let scroller = true;
      let scrollSpeed = 0;
      let  parentElement= null;
      let childElement = null;
      let viewHeight = 0;

      const toggleScrollEvent = function (e){
            scroller = (e.type == 'mouseenter') ? false : true;
        }
     

     const  setProperty =  function(element,obejct){

          viewHeight  = document.querySelector(element).clientHeight
       
            scrollSpeed = obejct.scrollSpeed;

            // create child element 
            childElement =  document.createElement('div');
            childElement.id = 'childElement';

           // append scroll content to  child element 
            let items = document.querySelectorAll(element+ ' .item');
            items.forEach(function(item){
              childElement.appendChild(item)
            });

            // append child element to parent element
            element = document.querySelector(element);
            parentElement = element.appendChild(childElement)

            // set child element top property value manually 200px
            // let hg = childElement.getBoundingClientRect();
            childElement.style.top = viewHeight+ 'px';


            
            parentElement.addEventListener('mouseenter',toggleScrollEvent);
            parentElement.addEventListener('mouseleave',toggleScrollEvent);

      } 


    const scrollEelement = function(){
      let posY = parseInt(childElement.style.top);
      // Get the first child div
      let firstDiv = childElement.querySelectorAll(".item")[0];
      // Check its height
      let firstDivHeight = firstDiv.clientHeight;
      // If we are scrolling...
      if(scroller){
        // Find out where the scroll would move to
        let tmppos = posY - scrollSpeed;
        // If it is higher than the height of the first div
        if (tmppos < 0 - firstDivHeight) {
          // remove the first div
          childElement.removeChild(firstDiv);
          // .. and append it to the end
          childElement.appendChild(firstDiv);
          // .. and do the scroll
          childElement.style.top = (tmppos + firstDivHeight) + "px";
        } else {
          // otherwise, just do the scroll
          childElement.style.top = tmppos + "px";
        }
      }
    }

   setInterval(scrollEelement,10)

    return {
      init:function(element,obejct){
       setProperty(element,obejct)
      }
    }

  })([]);

  window.addEventListener("DOMContentLoaded",function(){
    scrollAnimation.init('#parentElement',{
      scrollSpeed:1,
    });
  })
代码语言:javascript
运行
AI代码解释
复制
 *,*::after,*::before{
        box-sizing: border-box;
        margin: 0;
        padding: 0;
      }
      body,html{
        margin: 0;
        padding: 0;
      }
    
      #parentElement{
        position: relative;
        width: 400px;
        background: #eeeeee;
        height: 200px;
        overflow: hidden;
        border: 2px solid royalblue;
      }
        #parentElement #childElement{
            padding: 10px;
            position: absolute;
            width: 100%;
            top: 0;
            left: 0;
      }
代码语言:javascript
运行
AI代码解释
复制
<div id="parentElement">
      <h1 class="item">Start</h1>
      <h1 class="item">Mid................<br>..............on two lines</h1>
      <h1 class="item">Mid</h1>
      <h1 class="item">Mid</h1>
      <h1 class="item">Mid</h1>
      <h1 class="item">Mid</h1>
      <h1 class="item">Mid</h1>
      <h1 class="item" style="background: orangered;">End</h1>
  </div>

票数 2
EN

Stack Overflow用户

发布于 2020-10-18 13:19:20

这是一个主要使用css动画的解决方案。

代码语言:javascript
运行
AI代码解释
复制
const containerElem = document.querySelector('.container');
const contentElem = document.querySelector('.content');
const contentCloneElem = contentElem.cloneNode(true);
containerElem.appendChild(contentCloneElem);

const pxPerSec = 100;
const contentHeight = contentElem.clientHeight;
containerElem.style.animationDuration = `${contentHeight / pxPerSec}s`;
代码语言:javascript
运行
AI代码解释
复制
.infiniteScroll {
  border: 2px solid blue;
  height: 150px;
  overflow: hidden;
}

.container {
  border: 2px solid red;
}

.content {
  border: 1px solid green;
}

@keyframes changePos {
  from {
    transform: translateY(0px);
  }
  to {
    transform: translateY(-50%);
  }
}

.container {
  animation-duration: 5s;
  animation-timing-function: linear;
  animation-name: changePos;
  animation-iteration-count: infinite;
  animation-direction: normal;
}

.container:hover {
  animation-play-state: paused;
}
代码语言:javascript
运行
AI代码解释
复制
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Infinite Scroll</title>
</head>
<body>
  <div class="infiniteScroll">
    <div class="container">
      <div class="content">
        <h1 class="item">Start</h1>
        <h1 class="item">Mid</h1>
        <h1 class="item">Mid</h1>
        <h1 class="item">Mid</h1>
        <h1 class="item">Mid</h1>
        <h1 class="item">Mid</h1>
        <h1 class="item">Mid</h1>
        <h1 class="item" style="background: orangered;">End</h1>
      </div>
    </div>
  </div>
</body>
</html>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64413307

复制
相关文章
勿套“公式”!谈谈UI设计中的字号,间距,大小等规律
静电说:新手在学习UI设计过程中,通常会陷入到一个误区中,就是把UI设计当算术题来做。比如经常会有同学问:UI界面中的字号最小是多少?UI界面模块中的间距有没有什么规则可以遵循?是不是一定要4的倍数?UI界面左右留白多少才合适,有规范吗?按钮的圆角多少才合适?
用户5009027
2020/09/01
4.6K0
编写 vuetify 全局 snackbar
components/snackbar.vue <template> <v-snackbar v-model="open" :bottom="bottom" :color="color" :timeout="2000">{{ msg }}</v-snackbar> </template> <script lang='ts'> import { Component, Vue } from "vue-property-decorator"; @Component export default cla
4O4
2022/04/25
1K0
VUE-Vuetify框架
Vue虽然会帮我们进行视图的渲染,但样式还是由我们自己来完成。这显然不是我们的强项,因此后端开发人员一般都喜欢使用一些现成的UI组件,拿来即用,常见的例如:
cwl_java
2020/02/11
4.3K0
vue vuetify 实现智能联想
使用 Vuetify 中的 v-menu 组件实现,控制光标焦点,在输入框获取的焦点时弹出联想词汇菜单,支持上下按键选中内容,菜单位置,样式按需调整即可
kirin
2020/11/03
8840
修改xshell的默认字间距和行间距
可能是不小心修改了xshell的某个配置,导致打开的会话中显示字间距和行间距都非常大,严重影响工作。 参照官方手册也不能修改正常,详见:http://www.xshellcn.com/wenti/xiugai-ziti.html。
编程随笔
2019/09/11
2.3K0
修改xshell的默认字间距和行间距
神经网络(TensorFlow)游乐场
TensorFlow游乐场是一个通过网页浏览器就可以训练的简单神经网络并实现了可视化训练过程的工具。可以去简单地模拟深度学习的过程。连线越粗表明权重越大。
里克贝斯
2021/05/21
1.5K0
神经网络(TensorFlow)游乐场
vuetify-使用详细入门教程
写在前面的话,公司工作很久了,一直都没有改过自己的技术栈,才觉得慢慢的落后于潮流,也知道自己的技术栈很老旧,想过要重构项目,但是项目周期时间一直不许,学习vue只能在下班的时间里面,这两年也断断续续的用过一些框架,但最后还是选中了vuetify。
王小婷
2020/05/26
7K0
vuetify-使用详细入门教程
Vue引入vuetify框架你需要知道的几点
2、在src目录中创建一个名为的文件夹plugins在里面,添加一个vuetify.js文件。代码如下
马克社区
2022/06/03
6480
[Office] 段落间距调整
选中需要设置的段落
轻舞飞扬SR
2021/02/24
2.4K0
[Office] 段落间距调整
Figma中的自动布局要怎么用?一篇文学会官方说明文件
静电说:Figma在最近的几次更新中,发表了全新的Auto Layout(自动布局)功能,要知道,在之前的自动布局功能中,我们只能使用很简单的布局效果(类似于Sketch中的自动布局),而本次Figma在更新后,自动布局更加的完善好用,可以做出非常多的响应式效果。
用户5009027
2020/12/02
9.3K0
Figma中的自动布局要怎么用?一篇文学会官方说明文件
PCB的安全间距如何设计?
PCB设计中有诸多需要考虑到安全间距的地方。在此,暂且归为两类:一类为电气相关安全间距,一类为非电气相关安全间距。
MCU起航
2020/06/29
1.3K0
设置UITextView的行间距
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/53261917
用户1451823
2018/09/13
2K0
解决 vuetify 中 icon 显示错乱问题
Vuetify 使用 Google's Roboto fon 和 Material Design Icons。安装这些软件的最简单方式是将他们的 CDN 包含在你的主 index.html 中。
4O4
2022/04/25
1.4K0
设置UILabel的行间距
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/53262085
用户1451823
2018/09/13
1.3K0
html导航栏自动调间距,HTML CSS导航栏间距[通俗易懂]
我做了一个CSS导航栏,但是在每个“navbar-item”之间,都有一点空间。我不希望那里有任何变化!有没有办法做到这一点,而不改变每个navbar-item的余裕?HTML CSS导航栏间距
全栈程序员站长
2022/09/01
5.6K0
inline-block空格间距问题的解决
真正意义上的inline-block水平呈现的元素间,换行显示或空格分隔的情况下会有间距,很简单的个例子:
javascript.shop
2019/09/04
8810
inline-block空格间距问题的解决
(转)iOS学习——UIlabel设置行间距和字间距
  在iOS开发中经常会用到UIlabel来展示一些文字性的内容,但是默认的文字排版会觉得有些挤,为了更美观也更易于阅读我们可以通过某些方法将UIlabel的行间距和字间距按照需要调节。
mukekeheart
2019/01/07
4K0
译|CSS中的间距,前端开发中各种设置间距的优点缺点及实例
如果两个或多个元素很接近,那么用户就会认为它们以某种方式属于彼此。当对多个设计元素进行分组时,用户可以根据它们之间的空间大小来决定它们之间的关系。没有间距,用户将很难浏览页面并知道哪些内容相关而哪些内容无关。
张张
2020/05/12
12.1K0
译|CSS中的间距,前端开发中各种设置间距的优点缺点及实例
TensorFlow游乐场及神经网络简介
本文将通过TensorFlow游乐场来快速介绍神经网络的主要功能。TensorFlow游乐场(http://playground.tensorflow.org)是一个通过网页浏览器就可以训练的简单神经网络并实现了可视化训练过程的工具。下图给出了TensorFlow游乐场默认设置的截图。
博文视点Broadview
2020/06/11
7660
【说站】css文字间距的使用
1、letter-spacing是字母间距,每一个汉字和每一个英文字母被当做一个字。
很酷的站长
2022/11/24
6510

相似问题

我如何使用Vuetify间距助手?

12

Vuetify图标大小

54

更改vuetify的字体大小

31

关于更改游乐场纹理的大小

11

itemselector -列的间距大小

19
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档