竖向滚动

本文介绍了一种使用JavaScript实现的网页元素连续滚动效果的方法。通过监听窗口加载完成事件,自动初始化滚动容器,并设置定时器来控制滚动速度。此外,还实现了鼠标悬停时暂停滚动的功能。
<script type="text/javascript">
//添加事件响应函数的函数,与本效果无关
function addEventSimple(obj,evt,fn){
 if(obj.addEventListener){
  obj.addEventListener(evt,fn,false);
 }else if(obj.attachEvent){
  obj.attachEvent('on'+evt,fn);
 }
}
addEventSimple(window,'load',initScrolling);
//保存想要滚动的容器
var scrollingBox;
var scrollingInterval;
//用于记录是否“滚到头”过一次
var reachedBottom=false;
//记录第一次滚到头时候的scrollTop
var bottom;
//初始化滚动效果
function initScrolling(){
 scrollingBox = document.getElementById("scrollText");
 //样式设置,与滚动基本无关,应该用CSS设置。
 scrollingBox.style.height = "60px";
 scrollingBox.style.overflow = "hidden";
 //滚动
 scrollingInterval = setInterval("scrolling()",50);
 //鼠标划过停止滚动效果
 scrollingBox.onmouseover = over;
 //鼠标划出回复滚动效果
 scrollingBox.onmouseout = out; 
}
//滚动效果
function scrolling(){
 //开始滚动,origin是原来scrollTop
 var origin = scrollingBox.scrollTop++;
 //如果到头了
 if(origin == scrollingBox.scrollTop){
  //如果是第一次到头
  if(!reachedBottom){
   scrollingBox.innerHTML+=scrollingBox.innerHTML;
   reachedBottom=true;
   bottom=origin;
  }else{
   //已经到头过,只需回复头接尾的效果
   scrollingBox.scrollTop=bottom;
  }
 }
}
function over(){
 clearInterval(scrollingInterval);
}
function out(){
 scrollingInterval = setInterval("scrolling()",50);
}
</script>
  <div id="scrollText">
    <h2>连续滚动示例标题</h2>
    <p>实例新闻,可以添加任何想要滚动的内容。包括图片等等。</p>
    <p><img src="../../images/logo.gif" alt="连续滚动示例图片" /></p>
    <p><img src="http://bbs.blueidea.com/images/default/logo.gif" alt="连续滚动示例图片" /></p>
    <p>……</p>
  </div>

在Vue中实现竖向滚动有多种方法: - **使用better - scroll**:在Vue中使用better - scroll可实现竖向滚动。当满足`this.menuIndexChange && pos.y <= 0`条件时,可通过`this.scrollY = Math.abs(Math.round(pos.y))`获取滚动距离[^1]。 - **手写列表竖向滚动**:可通过自定义组件实现。在js层定义组件的`props`、`data`、`computed`和`methods`等。在`mounted`钩子中初始化信息并设置定时器,定时器内调用滚动方法。滚动方法中每次滚动20px,同时循环数据并通过`transform`属性实现滚动效果。示例代码如下: ```vue <template> <div> <ul ref="ul" :style="ulStyle"> <li ref="li" v-for="(item, index) in list" :key="index">{{ item }}</li> </ul> </div> </template> <script> export default { props: { list: { type: Array, require: true }, scrolStyle: Object, showMask: Boolean }, data() { return { config: { height: null }, step: 0, liIndex: 0 }; }, mounted() { this.initInfo(); setInterval(() => { this.$nextTick(() => { this.scrol(); }); this.liIndex++; }, 500); }, computed: { ulStyle() { return `height: ${this.config.height}px`; } }, methods: { initInfo() { this.config.height = this.$refs.li[0].clientHeight * this.list.length; }, scrol() { this.step += 20; this.list.push(this.list[this.liIndex]); this.$refs.ul.style.transform = `translateY(-${this.step}px)`; } } }; </script> ``` - **使用Swiper进行滚动**:在Vue3中可使用Swiper实现竖向滚动。参考文档为https://swiperjs.com/vue ,由于高版本(10.0.2)引入Autoplay会报错,可下载7.4.1版本(`npm install swiper@7.4.1`) [^3]。 - **自定义竖向文字滚动**:可自定义设置滚动文字数组(`sliderData`)、文字滚动时间间隔(`interval`,默认3000ms)、展示文字宽度(`width`,默认800px,可传100vw)、展示文字高度(`height`,默认60px)等属性 [^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值