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. IDEA 逆向工程

    今天要写一个导入功能,需要把数据库的一个表映射成一个pojo,但是这表字段不是一般的多啊...想了想能不能自动生成???在IDEA中点了半天发现还是可以的 下面介绍一下步骤: 第一步:找到这个图标,建 ...

  2. oracle添加用户并给予管理员身份登陆

    sqlplus / as sysdba;--超级管理员sys登陆 show user;--显示当前用户 conn system/admin;--切换用户 CREATE user admin ident ...

  3. 拓扑排序 - hdu 1285(普通和优先队列优化)

    2017-09-12 19:50:58 writer:pprp 最近刚开始接触拓扑排序,拓扑排序适用于:无圈图的顶点的一种排序, 用来解决有优先级别的排序问题,比如课程先修后修,排名等. 主要实现:用 ...

  4. 编译binutil包报错 error: array type has incomplete element type extern const struct relax_type md_relax_table[];

    安装lfs时编译binutils出错: ../../sources/binutils-2.15.91.0.2/gas/config/tc-i386.h:457:32: error: array typ ...

  5. Global.asax详解

    在网上找了N多相关的东西总说的不够细,现在终于找到了.可以了解web.cofig和Global.asax之间的关系以及执行的顺序. 在Global.asax.cs文件中 protected void ...

  6. KVM irqfd and ioeventfd

    In previous article vhost architecture we mentioned that vhost and the guest signal each other by ir ...

  7. 使用location.hash保存页面状态

    hash 属性是一个可读可写的字符串,该字符串是 URL 的锚部分(从 # 号开始的部分). 语法 location.hash 在我们的项目中,有大量ajax查询表单+结果列表的页面,由于查询结果是a ...

  8. python学习大纲目录(转自alex博客https://www.cnblogs.com/alex3714/)

    day01: 介绍.基本语法.流程控制 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 ...

  9. python sort与sorted使用笔记

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...

  10. vue项目搭建 (二) axios 封装篇

    vue项目搭建 (二) axios 封装篇 项目布局 vue-cli构建初始项目后,在src中进行增删修改 // 此处是模仿github上 bailicangdu 的 ├── src | ├── ap ...