element显示true或者false_element-ui轮播的简单实现
这段时间,一直在学习些源码,想了解下大佬的思路,看完后,只能说卧槽,还有这种操作。
element-ui一个Vue常用的组件库,包含很多的知识点可以学习,其很多组件只是样式的变化。难度比较大的,轮播算一个。今天就给大家简单实现下它的一系列操作。
首先它分为两个组件,一个是父组件(容器),另一个是子组件(幻灯片),首先容器是固定一个窗口,让对应的幻灯片显示。
我们先来写好它的模版。
父组件
<div class="n-slide"><div class="n-slide-container" :style="{height: height}"><slot></slot></div>
</div>
.n-slide-container {position: relative;overflow: hidden;}
子组件
<template><div v-show="ready" :class="['n-slide-item',{animating}]" :style="translateStyle"><slot></slot></div>
</template>
.n-slide-item {position: absolute;top: 0;left: 0;width: 100%;height: 100%;overflow: hidden;z-index: 0;&.animating {transition: transform 0.4s ease-in-out;}}
容器样式参数,只接受一个高度,宽度是自适应的。
这是文档上记录的可传递的参数。
下面我们按照,这些传递的参数简单实现下它的功能。
<script>export default {name: 'Slide',props: {loop: {type: Boolean,default: true},autoplay: {type: Boolean,default: true},height: String,interval: {type: Number,default: 3000},initialIndex: {type: Number,default: 0}},}
暂时,我们就只需要这些。实现一下自动滚动的功能。
首先我们需要加载幻灯片,看看有多少个:
父组件
data() {return {items: [],}
},
methods: {upDataItems() {this.items = this.$children.filter(child => child.$options.name === 'NSlideItem')}}
子组件
created() {this.$parent && this.$parent.upDataItems()}
这里加载幻灯片是通过,子组件的created
函数调用父组件的upDataItems
函数实现的。
这里如果不懂的同学,可以好好了解下Vue的生命周期,当父组件和子组件在一起时,生命周期是怎样的运作过程。就不详细阐述了。
加载完后,我们需要让轮播滚动起来。
父组件
mounted() {// 为了确保幻灯片加载完this.upDataItems()// $nextTick是把执行逻辑放入Vue的异步队列中,等Vue的事件初始化完成后才会执行,为了正确的给activeIndex赋值,否则初始化轮播为最后一张this.$nextTick(() => {if (this.initialIndex < this.items.length && this.initialIndex >= 0) {this.activeIndex = this.initialIndex;}// 打开定时器this.startTimer();});},
data: {items: [],timer: null,initIndex: 0, // 初始的indexactiveIndex: -1 // 滚动的index
},
methods: {startTimer() {this.timer = setInterval(this.playSlides, this.interval)},playSlides() {const items = this.items// 轮播滚动到最后一个就循环if (items.length && this.activeIndex < items.length - 1) {this.activeIndex++} else if (this.loop) {this.activeIndex = 0}},
},
这样在一加载完组件后,activeIndex
开始递增,到了最后一个就重复,下面需要一个监听器,监听activeIndex
的变化,然后去执行页面视图的变化。
watch: {autoplay(val) {if (val) {this.startTimer()}},activeIndex(activeIndex, oldIndex) {// 重新更新幻灯片的位置(主要函数)this.resetPositionItems(activeIndex, oldIndex)}},methods: {resetPositionItems(activeIndex, oldIndex) {this.items.forEach((item, index) => {item.initTranslate(index, activeIndex, oldIndex)})},}
子组件
name: 'NSlideItem',
data() {return {translate: 0,ready: false,animating: false}
},
methods: {// 处理循环轮播processIndex(index, activeIndex, length) {// 如果是最后一张都第一张if (activeIndex === 0 && index === length - 1) {return -1;// 如果是第一张到对后一张} else if (activeIndex === length - 1 && index === 0) {return length;} else if (index < activeIndex - 1 && activeIndex - index >= length / 2) {return length + 1;} else if (index > activeIndex + 1 && index - activeIndex >= length / 2) {return -2;}return index;},initTranslate(index, activeIndex, oldIndex) {const length = this.$parent.items.length;if (index !== activeIndex && length > 2 && this.$parent.loop) {index = this.processIndex(index, activeIndex, length);}this.animating = activeIndex === indexthis.translate = this.calcTranslate(index, activeIndex)// 组件初始化完this.ready = true},// 计算位移的距离,重点就是这个函数calcTranslate(index, activeIndex) {// 获取父组件的宽度const distance = this.$parent.$el.offsetWidth// 可以仔细思考下这个计算return distance * (index - activeIndex)}}
},
computed: {translateStyle() {// this.translate变化时,返回对应的stylereturn {transform: `translateX(${this.translate}px)`}}
},
created() {this.$parent && this.$parent.upDataItems()
}
这样轮播就实现了。测试一下
<template><div style="width: 500px"><n-slide height="400px"><n-slide-item v-for="(item,index) of ['red','grey','black','orange']"><div class="item" :style="{height: '100%',background: item,color: '#ffffff',fontSize: '30px'}">{{index+1}}</div></n-slide-item></n-slide></div>
</template>
element显示true或者false_element-ui轮播的简单实现相关推荐
- html渐变轮播,html简单的渐变轮播插件
html如何实现简单酷炫的简单的渐变轮播插件呢?这篇文章能够教你!感兴趣的话请看下文. 话不多说,请看代码: Example .CreabineCarousel{ width: 100%; heigh ...
- android轮播图简单实现(左右无限滑动,自动轮播)
直接上代码了,都有注释,原理很简单 public class MainActivity extends AppCompatActivity { private static final String ...
- html文字图片一起轮播卡片,简单的堆叠卡片样式jQuery轮播图插件
MxSlider.js是一款堆叠卡片样式的jQuery响应式轮播图插件.该轮播图插件兼容IE8浏览器,采用响应式设计,它在图片切换时,就像一叠扑克牌将最上面一张挪开的效果. 使用方法 在页面中引入Mx ...
- vue上下轮播组件简单实现
在vue社区里面没有找到特别好的上下轮播插件,基本都是图片的左右播放插件,质量也是参差不齐 在实现这个组件之前,先抛出一个问题,如何在vue中实现dom的环形结构? 首先来看轮播组件的思路 红色部分: ...
- 详细简单的php图片轮播,最简单的JavaScript图片轮播代码(两种方法)_javascript技巧...
通过改变每个图片的opacity属性: 素材图片: 代码一: 最简单的轮播广告 代码二: 首先第一步,下载好一个jquery库的插件,jquery.js 网上很多随处可以下载.下载的插件要放在目录下. ...
- html 图片轮播实例,简单实现轮播图效果的实例
一.要点: 1.页面加载时,图片重合,叠在一起[绝对定位]; 2.第一张显示,其它隐藏; 3.设置下标,给下标设置颜色让它随图片移动; 4.鼠标移动到图片上去,显示左右移动图标,鼠标移走,继续轮播; ...
- android 图片轮播圆点,简单的图片轮播 加小圆点
先说一下思路,使用ViewPager加 LinearLayout 布局 共同实现, 先看布局 android:layout_width="match_parent" androi ...
- richtextbox自动滚动到最下面_Axure RP 9教程:banner轮播最简单的实现方法
图片轮播是各大网站常常见到的形式,文章带我们学习了如何用Axure实现图片轮播功能,一起来看看~ 最近开始做产品的原型设计,从RP8到RP9的过渡,对于RP9的很对设置还比较陌生,很多东西还在摸索中. ...
- element显示true或者false_vue+element-ui实现行数可控的表格输入
element的table中使用 包裹想要插入的input,或者select等HTML元素,绑定一个的数组对象,在input或者select等HTML元素使用 v-model="scope. ...
最新文章
- gitlab解决一些问题
- latex 公式转图片
- (原創) 深入探討blocking與nonblocking (SOC) (Verilog)
- Python即Python解释器的发展史
- php 访问网页返回值,Ping网站并用PHP返回结果
- python中如何输出中文_python中怎么输出中文-问答-阿里云开发者社区-阿里云
- 命令点亮硬盘灯_macOS下移动硬盘无法挂载且硬盘灯一直闪烁的解决方法
- php aws ses,python-在AWS SES上接收和解析电子邮件
- ZOJ 3985 2017CCPC秦皇岛 E:String of CCPC
- maven配置ojdbc14
- 【吐血整理,建议收藏】B站上有哪些值得反复观看的Java视频教程?
- 4. 利用MySQL Shell安装部署MGR集群 | 深入浅出MGR
- 抢先看,2023年前瞻版Java八股文面试题,面试应该是够用了(吊打面试官)
- 【安卓开发系列 -- APP】APP 开发基础技术整理
- 给博客增加豆瓣观影和阅读
- 三、使用buildroot制作mke2fs (mkfs.ext2、mkfs.ext3、mkfs.ext4)
- 全球与中国二甲酮市场深度研究分析报告
- openwrt怎么做ap_OPENWRT的中继或者桥接模式, Client+AP实现
- 张朝阳:诚惶诚恐才能生存
- 一文解决中文在Eclipse中显示乱码的问题
热门文章
- 上海大学计算机组成原理12,上海大学计算机组成原理报告1.doc
- webpack中使用jquery
- Unity 游戏框架搭建 (七) 减少加班利器-QApp类
- 从数组里挑出仅仅出现一次的对象
- 第二轮冲刺-Runner站立会议03
- POJ 1753 Flip Game (黑白棋) (状态压缩+BFS)
- Photon——Setup and Config 设置与配置
- [网络安全自学篇] 九十.远控木马详解及APT攻击中的远控和防御
- [Python人工智能] 三.TensorFlow基础之Session、变量、传入值和激励函数
- 【数据结构与算法】之深入解析“打家劫舍III”的求解思路与算法示例