vue 使用swiper轮播图

代码精灵
• 阅读 3907

vue 使用swiper轮播图,自动轮播时鼠标移入暂停,鼠标移出再次播放,并给出多个轮播的处理方法以及后台获取数据时无法循环轮播解决方案

1、安装 vue-awesome-swiper
使用vue开发界面时,很多时候需要用到轮播图,有的时候需要在一个页面中使用多个轮播图,这个时候就需要用到单独的,鼠标移入暂停自动轮播,鼠标移出再次自动轮播。

1、npm install vue-awesome-swiper

2、代码部分
<template>

<div class="icontent">
    <div @mouseenter="on_top_enter" @mouseleave="on_top_leave">
        <!-- 加上v-if="banner_data.length > 1" 来防止swiper出现无法循环播放的效果 -->
        <swiper class="my-swiper" v-if="banner_data.length > 1" :options="swiperOption" ref="mySwiper">
        <!-- slides -->
        <swiper-slide v-for="ba_data in banner_data">
            <img :src="ba_data.img_url" alt="banner">
        </swiper-slide>
        <!-- 分页控制器 -->
        <div class="swiper-pagination"  slot="pagination"></div>
        <!-- 上一页 -->
        <div class="swiper-button-prev" slot="button-prev"></div>
        <!-- 下一页 -->
        <div class="swiper-button-next" slot="button-next"></div>
        </swiper>
    </div>
    <div class="my-swiper" @mouseenter="on_bot_enter" @mouseleave="on_bot_leave">
        <swiper class="bafang_swiper" v-if="bannerbottom_data.length > 1" :options="swiperOption" ref="myBotSwiper">
            <!-- slides -->
            <swiper-slide v-for="bot_data in bannerbottom_data">
            <img :src="bot_data.img_url" alt="bot_banner">
            </swiper-slide>
            <div class="swiper-pagination"  slot="pagination"></div>
            <div class="swiper-button-prev" slot="button-prev"></div>
            <div class="swiper-button-next" slot="button-next"></div>
        </swiper>
    </div>
</div>

</template>

<script>

import Vue from 'vue'
// 导入相关的包
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
import $ from 'jquery'
// 使用模块
Vue.use(VueAwesomeSwiper)
export default {
    name: "index",
    data () {
        return {
            msg: 'index',
            // 配置轮播图的相关参数 使用时绑定到标签里的 options 属性
            swiperOption: {
                // some swiper options/callbacks
                // 所有的参数同 swiper 官方 api 参数
                // 设置分页器
                pagination: {
                    el: '.swiper-pagination',
                    // 设置点击可切换
                    clickable :true
                },
                // 设置前进后退按钮
                navigation: {
                    nextEl: '.swiper-button-next',
                    prevEl: '.swiper-button-prev',
                },
                // 设置自动轮播
                autoplay: { 
                    delay: 2000,
                    disableOnInteraction: false,
                },
                // 设置轮播可循环
                loop : true
            },
            banner_data: [
                {img_url:'http://img.bimg.126.net/photo/ZZ5EGyuUCp9hBPk6_s4Ehg==/5727171351132208489.jpg'},
                {img_url:'http://img.juimg.com/tuku/yulantu/140218/330598-14021R23A410.jpg'},
                {img_url:'http://h.hiphotos.baidu.com/lvpics/w=1000/sign=bb4d3e46ecf81a4c2632e8c9e71a6159/77094b36acaf2edd14b2c5db8a1001e939019345.jpg'}
            ],
            bannerbottom_data: [
                {img_url:'http://img.bimg.126.net/photo/ZZ5EGyuUCp9hBPk6_s4Ehg==/5727171351132208489.jpg'},
                {img_url:'http://img.juimg.com/tuku/yulantu/140218/330598-14021R23A410.jpg'},
                {img_url:'http://h.hiphotos.baidu.com/lvpics/w=1000/sign=bb4d3e46ecf81a4c2632e8c9e71a6159/77094b36acaf2edd14b2c5db8a1001e939019345.jpg'}
            ]
        }
    },
    methods: {
        //通过获得的swiper对象来暂停自动播放
        on_bot_enter() {
            this.myBotSwiper.autoplay.stop()
        },
        on_bot_leave() {
            this.myBotSwiper.autoplay.start()
        },
        on_top_enter() {
            this.mySwiper.autoplay.stop()
        },
        on_top_leave() {
            this.mySwiper.autoplay.start()
        }
    },
    //计算属性,获得可以操作的swiper对象
    computed: {
        // 获取当前的swiper对象
        mySwiper() {
            // mySwiper 是要绑定到标签中的ref属性
            return this.$refs.mySwiper.swiper     
        },
        myBotSwiper() {
            return this.$refs.myBotSwiper.swiper
        }
    },
    created: function() {
    }
}

</script>
<style scoped>

.icontent{
    overflow: hidden;
    width: 100%;
}
.my-swiper{
    height: 500px;
}
.my-swiper:hover{
    cursor: pointer;
}
.my-swiper img{
    height: 100%;
    width: 100%;
}

</style>

点赞
收藏
评论区
推荐文章
CuterCorley CuterCorley
4年前
Django+Vue开发生鲜电商平台之11.首页、商品数量、缓存和限速功能开发
青,取之于蓝而青于蓝;冰,水为之而寒于水。——《荀子·劝学》Github和Gitee代码同步更新:;。一、首页功能完善首页待完善的功能包括轮播图、新品尝鲜、系列商品等。1.轮播图实现轮播图包括3张图片,链接对应3个商品,先在apps/goods/serializers.py中定义序列化如下:pythonclassBanner
昔不亏 昔不亏
4年前
「Vue — 插件」轮播图vue-awesome-swiper
1:安装cnpminstallvueawesomeswiper@3savedevcnpminstallswiper@32:在main.js中jsimportVueAwesomeSwiperfrom'vueawesomeswiper'import'swiper/dist/css/swiper.css'Vue.use(Vu
红橙Darren 红橙Darren
4年前
RecyclerView更全解析之 - 为它优雅的添加头部和底部
1.概述上一期的,解决了几个坑。那么这一期我们来动态为RecyclerView去加载头部和底部,为上一期的RecyclerView列表数据添加广告轮播图,至于广告轮播大家可以看一下这一期,这里我就不多讲了,直接拿过来用。    视频讲解:相关文章:                        
昔不亏 昔不亏
4年前
「Vue — 插件」轮播图vue-awesome-swiper
1:安装cnpminstallvueawesomeswiper@3savedevcnpminstallswiper@32:在main.js中jsimportVueAwesomeSwiperfrom'vueawesomeswiper'import'swiper/dist/css/swiper.css'Vue.use(Vu
Stella981 Stella981
3年前
Flutter Swiper制作轮播效果
!(https://oscimg.oschina.net/oscnet/71c82559e8c6c5a42d6e3420594398793d7.png)1、引入flutter\_swiper插件flutter最强大的siwiper,多种布局方式,无限轮播,Android和IOS双端适配.Flutter\_swiper的GitHub
Stella981 Stella981
3年前
Javascript使用三大家族和事件来DIY动画效果相关笔记(四)
1.图片轮播基础之缓速轮播◆使用封装的缓慢速动画来DIY滑动轮播图,也就是鼠标移动到123456这些数字上后,图片以缓慢速滑动的方式进行切换。<!DOCTYPEhtml<htmllang"en"<head<metacharset"UTF8"<title使用封装的缓速
Stella981 Stella981
3年前
Android开发工程师文集
Android开发工程师文集Fragment,适配器,轮播图,ScrollView,Gallery图片浏览器,Android常用布局样式FragmentFragmentManagerfragmentManagergetFragmentManager();FragmentTransactionbeg
Wesley13 Wesley13
3年前
JS实现小图放大轮播效果
JS实现小图放大轮播页面效果入下(图片为优行商旅页面照片):!(https://oscimg.oschina.net/oscnet/fea423c49d5f7430375068501d3f032d631.png) 实现效果:图片自动轮播,鼠标移入停止,移出继续轮播点击下方小图可以实现切换步骤一:建立HTML布局,具体
程序员一鸣 程序员一鸣
2个月前
HarmonyOs开发:轮播图Banner组件封装与使用
轮播图在每个项目中都很常见,鸿蒙中在容器组件中也提供了Swiper组件,用于子组件滑动轮播显示,和前端的使用起来也是异曲同工,我们先看下基本的用法。
布局王 布局王
1个月前
Uniapp开发鸿蒙购物项目实战教程:实现首页轮播图
过去几天的文章中我们讲过了如何创建跨平台项目,如何进行基础的布局、如何实现自定义导航栏等等,通过这一系列的文章教程,我们最终要实现一个购物app,今天我们要做购物应用首页的轮播图部分。对于轮播图uniapp提供了相应的组件swiper,并且是支持鸿蒙应用的
布局王 布局王
2个月前
Uniapp开发鸿蒙购物项目教程之样式选择器
大家下午好,今天依然为大家带来鸿蒙跨平台开发教程的分享,我们本系列的教程最终要做一个购物应用,通过这个项目为大家分享uniapp开发鸿蒙应用从配置开发环境到应用打包上架的完成过程。昨天的文章实现了应用首页的轮播图,其中涉及到为轮播图设置样式,放一小段代码带