vue开发中遇到这样一个需求实现导航栏和中间内容相结合实现页面滑动导航跟随改变的效果。看效果:
这里我用的是vue所带的插件:vue-awesome-swiper,传送门:https://www.npmjs.com/package/vue-awesome-swiper
这个api纯英文,不过不是很难理解,大家只需要看那些地方有需要,可以copy=>paste到自己的地方。
准备:
1.node 环境 vue环境
2.vue init webpack project
3.npm install vue-awesome-swiper   
这里讲解下上述demo的实现的思路
一:首先需要先实现导航的点击变色
这个我们只需要添加自定义属性,分别给定下标0,1,2添加同一个点击事件。
通过传值$event获得自定义属性,但后动态添加类名,给改类名设置样式即可。
 
二:我们要实现点击导航滑块滑倒对应的位置
我们只需要调用api:this.swiper.slideTo(res.target.dataset.current, 1000, false);第一个参数为下标,1000=1s
点击的下标赋给第一个参数即可实现点击导航到对应的滑块。
三:实现滑块滑动改变data属性,改变类名所依赖的判断:
这里我们可以给最外层的swiper标签添加属性:@slideChangeTransitionEnd="moveEnd('滑块移动结束')"
通过调用moveEnd,再滑块结束后通过this.swiper获取元素当前的下标记得是activeIndex;
然后将改为类名所以来的virtualDom即可。
思路整理就是这样,下面附上主要代码:
main.js:
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
Vue.use(VueAwesomeSwiper, /* { default global options } */)
Vue.config.productionTip = false /* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
index.vue
<template>
<div class="swiper">
<a href="#" :class="currentTab==0?'focus':''" data-current="0" @click="switchTab('点击了slider1',$event)">slider1</a>
<a href="#" :class="currentTab==1?'focus':''" data-current="1" @click="switchTab('点击了slider1',$event)">slider2</a>
<a href="#" :class="currentTab==2?'focus':''" data-current="2" @click="switchTab('点击了slider1',$event)">slider3</a>
<swiper :options="swiperOption" ref="mySwiper" @slideChangeTransitionEnd="moveEnd('滑块移动结束')">
<swiper-slide v-for="(item, index) in slides" :key="index"><img :src="item"></swiper-slide>
</swiper>
</div>
</template>
<script>
import { swiper, swiperSlide } from 'vue-awesome-swiper';
export default{
name: 'test2',
data(){
return {
currentTab: 0,
slides: [
'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1511015180050&di=0d2ee92eead284e8133d6df07535d75a&imgtype=0&src=http%3A%2F%2Fimg.sc115.com%2Fuploads1%2Fsc%2Fjpgs%2F1512%2Fapic16988_sc115.com.jpg',
'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1511015180167&di=7412fd486c47c15f1d27485be0d7bd28&imgtype=0&src=http%3A%2F%2Fwww.duoxinqi.com%2Fimages%2F2012%2F06%2F20120605_8.jpg',
'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1511015180167&di=3bcedd33a30129b9951be2a81f9b505c&imgtype=0&src=http%3A%2F%2Fpic1.5442.com%2F2015%2F0420%2F06%2F05.jpg'
],
//轮播config
swiperOption: {
// 如果自行设计了插件,那么插件的一些配置相关参数,也应该出现在这个对象中,如下debugger
debugger: true,
// autoplay: true,
// loop: true,
slidesPerView: "auto",//设置slider容器能够同时显示的slides数量(carousel模式)。可以设置为数字(可为小数,小数不可loop),或者 'auto'则自动根据slides的宽度来设定数量。loop模式下如果设置为'auto'还需要设置另外一个参数loopedSlides。
centeredSlides: true,//<span style="color:rgb(68,68,68);font-family:'microsoft yahei';font-size:13px;">设定为true时,活动块会居中,而不是默认状态下的居左。</span>
hashNavigation: true,
}
}
},
methods: {
switchTab: function (prompt,res) {
// console.log(prompt,res.target.dataset.current);
this.currentTab = res.target.dataset.current;
this.swiper.slideTo(res.target.dataset.current, 1000, false)
},
moveEnd: function(prompt){
console.log(prompt);
let oIndex = this.swiper.activeIndex;
console.log(oIndex);
this.currentTab = oIndex;
// if(oIndex <= 2) {
// this.currentTab = oIndex;
// } else {
// this.currentTab = oIndex - 3;
// }
}
},
mounted() {
console.log('this is current swiper instance object', this.swiper);
},
components: {
swiper,
swiperSlide
},
computed: {
swiper () {
return this.$refs.mySwiper.swiper
}
}
} </script>
<style scoped>
.swiper {
margin: 10px auto;
width: 10rem;
height: 6.4rem;
/*overflow: hidden;*/
}
.swiper .swiper-slide {
width: 8.53rem;
height: 6.4rem;
}
.swiper-slide.swiper-slide-active img{
margin-top: 0;
width: 100%;
height: 100%;
}
.swiper img {
display: block;
margin: 0 auto;
margin-top: 3.5%;
width: 90.635%;
height: 90.625%;
vertical-align: middle;
-webkit-transition: all .5s ease 0s;
-moz-transition: all .5s ease 0s;
-ms-transition: all .5s ease 0s;
-o-transition: all .5s ease 0s;
transition: all .5s ease 0s;
}
.focus {
color: red;
}
</style>
总结至此,vue实现轮播图的添加算完成,希望对小伙伴们有用,记得点赞哦。

vue轮播图的更多相关文章

  1. vue轮播图插件vue-awesome-swiper的使用与组件化

    不管是APP还是移动端网页开发,轮播图在大部分项目当中都是存在的,这时候如果用vue开发项目,选择一款好的插件并且封装好是很重要的 1. 推荐使用vue-awesome-swiper 安装:cnpm ...

  2. Vue轮播图插件---Vue-Awesome-Swiper

    轮播图插件 Vue-Awesome-Swiper 地址:https://github.com/surmon-china/vue-awesome-swiper 安装:npm install vue-aw ...

  3. 做一个vue轮播图组件

    根据huangyi老师的慕课网vue项目跟着做的,下面大概记录了下思路 1.轮播图的图 先不做轮播图逻辑部分,先把数据导进来,看看什么效果.在recommend组件新建一个recommends的数组, ...

  4. vue轮播图插件之vue-awesome-swiper

    移动端轮播图插件,在使用iview图形界面插件中的carousel组件无法实现触摸滑动后,转而使用vue-awesome-swiper插件 1.npm安装 npm i vue-awesome-swip ...

  5. vue --轮播图

    轮播图,可以使用mint-ui中的swipe HTML: <Swipe :auto="4000"> <SwipeItem v-for="item in ...

  6. vue轮播图实现

    1.先安装组件 cnpm install vue-awesome-swiper; 2.在main.js下引入文件: import VueAwsomeSwiper from 'vue-awesome-s ...

  7. vue 轮播图插件 vue-awesome-swiper

    1.npm安装 npm install vue-awesome-swiper --save 2.vue 引入 //在main.js 中全局引入 import VueAwesomeSwiper from ...

  8. vue轮播图中间大两头小

    <template> <div v-if="items.length" class="full-page-slide-wrapper"> ...

  9. vue music-抓取歌单列表数据(渲染轮播图)

    下载安装新依赖 babel-runtime:对es6语法进行转译 fastclick:对移动端进行点击300毫秒延迟 ,,取消掉 babel-polyfill:API 先添加,在npm install ...

随机推荐

  1. 在 Linux 服务器上搭建和配置 Hadoop 集群

    实验条件:3台centos服务器,jdk版本1.8.0,Hadoop 版本2.8.0 注:hadoop安装和搭建过程中都是在用户lb的home目录下,master的主机名为host98,slave的主 ...

  2. apache配置ssl

    1.确认是否安装ssl模块 是否有mod_ssl.so文件   2.生成证书和密钥   linux下 步骤1:生成密钥 命令:openssl genrsa 1024 > server.key 说 ...

  3. eclipse及tomcat web站點

    eclipse环境下如何配置tomcat https://www.cnblogs.com/Leo_wl/p/4769760.htmleclipse环境下如何配置tomcat,并且把项目部署到Tomca ...

  4. SSM整合报错org.springframework.beans.factory.UnsatisfiedDependencyException

    我解决的办法是把.m2仓库所有文件删除,重新maven project就可以了. 但是在做这一步之前,报错如下: ①org.springframework.beans.factory.Unsatisf ...

  5. oracle 11g安装过程中问题:移动bin\oralbac11.dll 到bin\oralbac11.dll.dbl出错

    解决方法: 直接找到oralbac11.dll.dbl这个文件,将其删除即可.   http://blog.sina.com.cn/s/blog_51beaf0e0101000v.html

  6. eclipse使用lombok

    1.下载lombok.jar,将lombok复制到eclipse的安装路径下,如图: 2.在eclipse.ini配置文件最后加入:-javaagent:D:\Program Files\Eclips ...

  7. hdu4305生成树计数

    先预处理出距离,然后判断是否可行,要注意判断是否在一条直线上时判断是在两侧还是一边(wa了四次) double型数据 #include<map> #include<set> # ...

  8. 大数据位图法(无重复排序,重复排序,去重复排序,数据压缩)之Java实现

    1,位图法介绍 位图的基本概念是用一个位(bit)来标记某个数据的存放状态,由于采用了位为单位来存放数据,所以节省了大量的空间.举个具体的例子,在Java中一般一个int数字要占用32位,如果能用一位 ...

  9. EASYUI 1.4版 combobox firefox 下不支持中文检索的问题

    easyui 的combobox 在IE下面输入中文,可以自动实现筛选和检索的功能,但是在firefox下面不可以. 于是查了一些资料,发现原来是浏览器对于中文输入法的处理问题,对于chrome 和 ...

  10. Android_布局属性大全

    RelativeLayout 第一类:属性值为true可false android:layout_centerHrizontal        水平居中 android:layout_centerVe ...