很久之前做小程序时有个类似每日优鲜里储值卡充值界面里的 卡轮播和价格tab栏联动效果,当时觉得新鲜做出来之后也没当回事。直到今天又遇到了一个类似的功能,所以想着总结经验。

实现效果如下图:

图解:点击tab菜单 三个选项时,下面的轮播会随之滑动,下面的商品列表也会根据上面的tab选项变化。反之,当左右滑动切换swiper时,tab选择会随之选中高亮,下面的商品列表也会跟着变化。

实现思路:tab选项的个数跟swiper 滑块数量一致。可以根据当前选中/滑块获取索引值,展现相应的选中项/滑块。下面要展示的商品列表,因为有下拉加载更多的需求,需要另一个接口完成实现。根据当前选中项的状态值作为参数,去请求相对应得列表。

实现代码如下:

1、首先在项目里 npm install swiper vue-awesome-swiper --save

2、在main.js全局引入

import Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper' // import style
import 'swiper/css/swiper.css' Vue.use(VueAwesomeSwiper, /* { default options with global component } */)

3、或者在当前页面局部引入

import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css' export default {
components: {
Swiper,
SwiperSlide
}
}

4、页面上使用如下:

<div class="coupon_bot">
<ul class="coupon_b_name">
<li v-for="(item,index) in cardList"
:key="index"
:class="{'actived':index == isActiveIndex}"
@click="handlerClick('cardChange',index)">
{{item.cardTypeName}}
</li>
</ul>
<swiper class="coupon_swiper swiper-container" :options="swiperOption" ref="mySwiper">
<swiperSlide v-for="(item,index) in cardList" :key="index" class="coupon_s_slider">
<div class="conpon_swiper_bg">
<ul class="con_swiper_content">
<li>
<span class="conpon_s_t">{{item.cardType == 1?'本月余额':'本月剩余'}}</span>
<span class="conpon_s_c" :class="{'conpon_price':item.cardType == 1}">{{item.notGet}}</span>
<i class="conpon_unit conpon_unit_ft" v-show="item.cardType != 1">张</i>
</li>
<li>
<span class="conpon_s_t">{{item.cardType == 1?'已用额度':'本月已用'}}</span>
<span :class="{'conpon_s_rprice':item.cardType == 1}" >{{item.isGet}}</span>
<i class="conpon_unit" v-show="item.cardType != 1">张</i>
</li>
<li>
<span class="conpon_s_t">{{item.cardType == 1?'本月限额':'本月总数'}}</span>
<span :class="{'conpon_s_rprice':item.cardType == 1}">{{item.ttlGet}}</span>
<i class="conpon_unit" v-show="item.cardType != 1">张</i>
</li>
</ul>
</div>
</swiperSlide>
</swiper>
</div>

5、data里初始化swiper

 swiperOption: {
autoplay: false,
loop: false,
spaceBetween : 10,
observer: true,
observeParents: true,
on: {
slideChangeTransitionEnd: function(){
_this.isActiveIndex = this.realIndex;//切换结束时,告诉我现在是第几个slide
_this.type = _this.cardList[_this.isActiveIndex].cardType;
_this.cardInfoList = [];
_this.pageNo = 1;
_this.isScroll = true;
_this.loading = true;
_this.getListData();
},
}
},

6、计算属性获取swiperDOM

 computed:{
swiper(){
return this.$refs.mySwiper.swiper
}
},

7、tab点击事件实现

handlerClick(action,data){
if(action == 'cardChange'){
//tab切换
this.loading = true;
this.isActiveIndex = data;
this.swiper.slideTo(this.isActiveIndex); //swiper滑动到相对应的滑块
document.documentElement.scrollTop = document.body.scrollTop = 0;
}
}

8、相对应的swiper scss样式如下

<style lang="scss" scoped>
@function rem($px) {
@return $px / 75 * 1rem;
}
$bg-color: #fff;
$text-color: #587BF3;
$money-color: #FD8040;
$title-color: #999;
.coupon_bot{
padding-top: rem(20);
.coupon_b_name{
display: flex;
color: $bg-color;
font-size: rem(32);
li{
flex: 1;
justify-content: space-between;
text-align: center;
padding: rem(10) 0;
}
.actived{
position: relative;
}
.actived::before{
content:'';
display: inline-block;
width: rem(64);
height: rem(4);
background: $bg-color;
border-radius: 2px;
position: absolute;
bottom: 0;
left: 50%;
margin-left: -13%;
}
}
.coupon_swiper{
padding: rem(30);
.coupon_s_slider{
width: rem(690) !important;
height: rem(200);
box-shadow: 0px rem(20) rem(30) 0px rgba(0, 0, 0, 0.06);
background: $bg-color;
border-radius: 16px;
.conpon_swiper_bg{
width: 100%;
height: 100%;
background: url('../../../static/images/coupon/swiperBg.png') no-repeat top right;
background-size: rem(485) rem(220);
.con_swiper_content{
padding: rem(50) rem(30);
display: flex;
justify-content: space-between;
li {
width: 33%;
text-align: center;
span{
color:#666;
font-weight: 500;
font-size: rem(40);
line-height: rem(48);
}
.conpon_s_t {
display: block;
color: $title-color;
font-size: rem(28);
line-height: rem(42);
margin-bottom: rem(30);
}
.conpon_s_c{
text-align: left;
display: inline-block;
color: #333;
font-size: rem(54);
}
.conpon_price{
font-size: rem(36);
}
.conpon_s_rprice{
font-size: rem(32);
}
.conpon_unit{
font-style: normal;
font-size: rem(28);
}
.conpon_unit_ft{
font-size: rem(36);
}
}
}
}
}
>>>.swiper-slide-active{
height: rem(220);
}
}
}
</style>

以上就实现了联动效果了(代码仅供参考)。

vue-awesome-swiper ---移动端h5 swiper 和 tab 栏选项联动效果实现的更多相关文章

  1. 16.vue-cli跨域,swiper,移动端项目

    ==解决跨域:== 1.后台 cors cnpm i -S cors 2.前端 jsonp 3.代理 webpack: myvue\config\index.js 找 proxyTable proxy ...

  2. vue移动端h5页面根据屏幕适配的四种方案

    最近做了两个关于h5页面对接公众号的项目,不得不提打开微信浏览器内置地图导航的功能确实有点恶心.下次想起来了的话,进行总结分享一下如何处理.在vue移动端h5页面当中,其中适配是经常会遇到的问题,这块 ...

  3. Swiper --移动端触摸滑动插件

    Swiper使用方法 1.首先加载插件,需要用到的文件有swiper.min.js和swiper.min.css文件. <!DOCTYPE html> <html> <h ...

  4. 使用swiper简单的h5下滑翻页效果,

    <!DOCTYPE html><html lang="en"><head>  <meta charset="utf-8" ...

  5. vue 的组件开发,以及swiper,axios的使用

    父组件<template> <div> <home-header :city="city"></home-header> //给子组 ...

  6. swiper移动端下不能正常轮播的解决方案-----此坑没躺过估计很难找到正确姿势

    <script> var mySwiper = new Swiper('.swiper-container', { direction: 'vertical', //horizontal横 ...

  7. Swiper 移动端全屏轮播图效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  8. 移动端 H5 页面注意事项

    1. 单个页面内容不能过多 设计常用尺寸:750 x 1334 / 640 x 1134,包含了手机顶部信号栏的高度. 移动端H5活动页面常常需要能够分享到各种社交App中,常用的有 微信.QQ 等. ...

  9. 移动端H5混合开发设置复盘与总结

    此篇接上一篇: 移动端H5混合开发,Touch触控,拖拽,长按, 滑屏 实现方案 https://www.cnblogs.com/buoge/p/9346699.html app 场布设置已经上线了, ...

随机推荐

  1. 第24课 - #pragma 使用分析

    第24课 - #pragma 使用分析 1. #pragma简介 (1)#pragma 是一条预处理器指令 (2)#pragma 指令比较依赖于具体的编译器,在不同的编译器之间不具有可移植性,表现为两 ...

  2. 内存管理初始化源码2:setup_arch

    PFN相关宏说明: /* kernel/include/linux/pfn.h */ PFN : Page Frame Number(物理页帧) /* * PFN_ALIGN:返回地址x所在那一页帧的 ...

  3. python面向对象单继承,多继承和super()调用

    python 目录 python 1.继承 1.单继承 2.多继承 3.子类重写父类的同名属性和方法 核心点: 4.多层继承 5.super()的使用 1.继承 1.单继承 说明: 虽然子类没有定义_ ...

  4. flutter权限管理permission_handler

    flutter权限管理permission_handler 添加依赖 #权限 permission_handler: ^3.0.0 使用 在android的mainfest中添加权限: <use ...

  5. Spring Cloud Alibaba生态探索:Dubbo、Nacos及Sentinel的完美结合

    @ 目录 背景 一.项目框架 1.1 采用IDEA和Maven多模块进行项目搭建 1.2 模块管理及版本管理 二.微服务公共接口 2.1 定义一个公共接口Api 2.2 pom.xml 2.3 Goo ...

  6. 谈谈 Java 中的那些“琐”事

    一.公平锁&非公平锁 是什么 公平锁:线程按照申请锁的顺序来获取锁:在并发环境中,每个线程都会被加到等待队列中,按照 FIFO 的顺序获取锁. 非公平锁:线程不按照申请锁的顺序来获取锁:一上来 ...

  7. Maven【常见知识点速查】

    文章更新时间:2020/04/10 一.为什么使用Maven这样的构建工具[why] ① 一个项目就是一个工程 如果项目非常庞大,就不适合使用package来划分模块,最好是每一个模块对应一个工程,利 ...

  8. Spring学习(二)Spring IoC 和 DI 简介

    一.IOC(控制反转) 定义:反转控制 (Inversion Of Control)的缩写,即创建对象的反转控制. 正向控制:若要使用某个对象,需要自己去负责对象的创建. 反向控制:若要使用某个对象, ...

  9. 生命周期(初始化、销毁方法、BeanPostProcessor后处理Bean)

    1.初始化和销毁 在目标方法执行前后进行初始化或销毁 (1)在Service方法的实现类里面创建初始化方法和销毁方法: public class StudentServiceImpl implemen ...

  10. Oracle 11gR2

    OracleOraDb11g_home1TNSListener #其它客服端连接需要开启服务,如不开启,本机连接可以直接使用sqlplus OracleServiceORCL #实例SID服务  sq ...