vue引入swiper插件的使用實例
本文介紹了vue引入swiper插件,分享給大家,希望對大家有幫助
步驟一:安裝vue,
$ npm install vue
步驟二:創(chuàng)建vue項目
# 全局安裝 vue-cli $ npm install -g vue-cli $ cd my-project $ npm install $ npm run dev
上面這些就是安裝好vue項目,最主要的就是下面的步驟
步驟三:下載好swiper相關(guān)的js和css,js放在static目錄下,css放在assets目錄下。
步驟四:
安裝runtime:
終端命令:npm install babel-runtime
步驟五:
修改.eslintrc.js文件如下:
// http://eslint.org/docs/user-guide/configuring
module.exports = {
root: true,
parser: 'babel-eslint',
parserOptions: {
sourceType: 'module'
},
env: {
browser: true,
},
// https://github.com/feross/standard/blob/master/RULES.md#javascript-standard-style
extends: 'standard',
// required to lint *.vue files
plugins: [
'html'
],
// add your custom rules here
'rules': {
// allow paren-less arrow functions
'arrow-parens': 0,
// allow async-await
'generator-star-spacing': 0,
// allow debugger during development
'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0
},
'globals': {
"Swiper": true
} //這個地方是新加入的 全局注入
}
步驟六:在自己的vue文件中添加輪播圖代碼
<div v-on:mouseenter="stopPlay()" v-on:mouseleave="play()" class="swiper-container gallery-top swiper-container-horizontal">
<div class="swiper-wrapper">
<div v-for="value in lbt" class="swiper-slide swiper-slide-next" style="width: 100%; margin-right: 10px;" v-bind:style="{backgroundImage: 'url(' + value.imgs + ')'}"></div>
</div>
<div class="swiper-button-next swiper-button-white"></div>
<div class="swiper-button-prev swiper-button-white swiper-button-disabled"></div>
</div>
<div class="swiper-container gallery-thumbs swiper-container-horizontal">
<div class="swiper-wrapper">
<div v-for="value in lbt" class="swiper-slide swiper-slide-next" style="margin-right: 10px;" v-bind:style="{backgroundImage: 'url(' + value.imgs + ')'}"></div>
</div>
</div>
import Swiper from '../../static/swiper-3.4.2.min.js'
let galleryTop
let galleryThumbs
export default {
name: 'main',
data () {
return {
lbt: [
{
'imgs': '../static/product/lbt1.jpg'
}, {
'imgs': '../static/product/lbt2.jpg'
}, {
'imgs': '../static/product/lbt3.jpg'
}
]
}
},
mounted () {
this.lunbo()
},
methods: {
lunbo () {
galleryTop = new Swiper('.gallery-top', {
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev',
spaceBetween: 10,
grabCursor: true,
initialSlide: 1,
autoplayDisableOnInteraction: false
})
galleryThumbs = new Swiper('.gallery-thumbs', {
spaceBetween: 10,
autoplay: 4000,
initialSlide: 1,
centeredSlides: true,
slidesPerView: 'auto',
touchRatio: 0.2,
slideToClickedSlide: true,
autoplayDisableOnInteraction: false,
grabCursor: true
})
galleryTop.params.control = galleryThumbs
galleryThumbs.params.control = galleryTop
},
stopPlay () {
galleryTop.stopAutoplay()
galleryThumbs.stopAutoplay()
},
play () {
galleryTop.startAutoplay()
galleryThumbs.startAutoplay()
}
}
}
@import url("../assets/swiper-3.4.2.min.css");
.gallery-top{
height:32rem;
width:100%;
}
.gallery-thumbs{
height:20%;
box-sizing:border-box;
padding:10px 0;
background: rgba(0, 0, 0, 0.4);
cursor: pointer;
}
.gallery-thumbs .swiper-slide{
width:30%;
height:6rem;
opacity:0.3;
}
.gallery-thumbs .swiper-slide-active{
opacity:1;
}
.swiper-slide{
background-size: 100% 160%;
-webkit-background-size: 100% 160%;
}
這里還有一個很重要的問題,在模板里面設(shè)置背景圖,寫法應(yīng)該是
v-bind:style="{backgroundImage: 'url(' + value.imgs + ')'}"
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue-admin-template?報Uncaught?(in?promise)?error問題及解決
這篇文章主要介紹了Vue-admin-template?報Uncaught?(in?promise)?error問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
@click.native和@click的區(qū)別及說明
這篇文章主要介紹了@click.native和@click的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
vue從后端獲取到文件的?url?地址及前端根據(jù)?url?地址下載文件的實現(xiàn)思路
這篇文章主要介紹了vue?中從后端獲取到文件的?url?地址及前端根據(jù)?url?地址下載文件,項目用的是?vben?admin?框架,用的是?vue3?+?TS,后端返回的是文件的?url?地址,對vue后端獲取?url?地址的相關(guān)知識感興趣的朋友一起看看吧2024-02-02
vue.config.js中配置configureWebpack和chainWebpack以及一些常用的配置
configureWebpack和chainWebpack都是Vue CLI中用于修改Webpack配置的工具,configureWebpack可以通過對象或函數(shù)修改配置,簡單直接;chainWebpack則使用WebpackChainAPI,適合復(fù)雜配置,兩者可以結(jié)合使用,以達到更精細的配置需求,幫助開發(fā)者優(yōu)化項目構(gòu)建2024-10-10

