vuejs 制作背景淡入淡出切換動(dòng)畫(huà)的實(shí)例
安裝好vuejs之后,在components里添加Background.vue
代碼如下
<template>
<div class="Background">
<div class="bg">
<transition
v-bind:css="false"
v-on:before-enter="beforeEnter"
v-on:enter="enter"
v-on:leave="leave">
<img v-bind:src="showImg" v-if="show" />
</transition>
</div>
<div class="screen"></div>
</div>
</template>
<script>
export default {
name: 'background',
data () {
return {
imgs: [],
isAnimate:false,
showImg: "static/bg/0.jpg",
showIndex: 0,
show: true
}
},
mounted:function(){
this.$nextTick(function () {
this.show=false;
this.bg_data();
});
},
methods:{
bg_data: function(){
var _this = this;
this.$http.get('static/data/bg.json').then(function(response){
_this.imgs = response.body;
});
},
beforeEnter: function (name) {
name.style.opacity=0;
name.style.transform = "scale(1) rotate(0deg)";
},
enter: function (name, done) {
var vm = this;
Velocity(name,
{ opacity: 1 ,
scale: 1.2,
rotateZ: "3deg"},
{
duration: 6000,
complete: function () {
done();
vm.show = false;
}
}
);
},
leave: function (name, done) {
var vm = this;
Velocity(name,
{ opacity: 0 ,
scale: 1,
rotateZ: "0deg"},
{
duration: 6000,
complete: function () {
done()
vm.showImg = vm.imgs[vm.showIndex==6 ? vm.showIndex=0 : vm.showIndex+=1 ].imgURL;
vm.show = true;
}
}
);
}
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.bg{
position: fixed;
left: 0px;
top:0px;
background-color: rgb(180, 180, 180);
height: 100%;
width: 100%;
min-width: 1000px;
z-index: -100;
background-position: center 0;
background-repeat: no-repeat;
background-size: cover;
-webkit-background-size: cover;
-o-background-size: cover;
zoom: 1;
}
img{
display: inline-block;
position: relative;
width: 100%;
height: 100%;
vertical-align: middle;
z-index: -99;
}
.screen{
width: 100%;
height: 100%;
background-color: #444;
z-index: -98;
opacity: 0.8;
filter:progid:DXImageTransform.Microsoft.Alpha(Opacity=10);
position: absolute;
top: 0px;
left: 0px;
}
</style>
圖片的json數(shù)據(jù)如下
[
{
"fileName" : "0.jpg",
"imgURL": "static/bg/0.jpg"
},
{
"fileName" : "1.jpg",
"imgURL": "static/bg/1.jpg"
},
{
"fileName" : "2.jpg",
"imgURL": "static/bg/2.jpg"
},
{
"fileName" : "3.jpg",
"imgURL": "static/bg/3.jpg"
},
{
"fileName" : "4.jpg",
"imgURL": "static/bg/4.jpg"
},
{
"fileName" : "5.jpg",
"imgURL": "static/bg/5.jpg"
},
{
"fileName" : "6.jpg",
"imgURL": "static/bg/6.jpg"
}
]
如果路由不會(huì)的話看一下網(wǎng)上的資料
碰到的問(wèn)題
1.在vue中想直接讓頁(yè)面加載時(shí)運(yùn)行函數(shù)的話將函數(shù)放在mounted對(duì)象里面。
2.函數(shù)放在methods 中
vue-resource用法 //用來(lái)獲取圖片的json數(shù)據(jù)
this.$http.get(url).then(response =>{
console.log(response.body);
},response =>{
console.log(response.body);
});
}
4.用vue-resource時(shí)需要把
import VueResource from 'vue-resource' Vue.use(VueResource);
寫(xiě)到main.js中去
5.mounted函數(shù)中,需要將運(yùn)行函數(shù)放在
this.$nextTick(function () {
.........
})
中
6.在vue中用velocity-animate
npm install velocity-animate --save -dev
在main.js中加入
import Velocity from 'velocity-animate'
7.多圖片循環(huán)過(guò)度效果
這里研究了很久,頁(yè)面進(jìn)去之后會(huì)直接從leave函數(shù)開(kāi)始運(yùn)行,不是想象的從beforeEnter開(kāi)始。后來(lái)終于弄清楚為什么了,把show: true改成show: false,則可以讓頁(yè)面從beforeEnter前開(kāi)始。
這個(gè)是參照vuejs的手冊(cè)的,http://cn.vuejs.org/v2/guide/transitions.html這里是關(guān)于過(guò)度效果的所有方面的東西。感覺(jué)能省很多代碼。
<div class="bg"> <transition v-bind:css="false" v-on:before-enter="beforeEnter" v-on:enter="enter" v-on:leave="leave"> <img v-bind:src="showImg" v-if="show" /> </transition> </div>
<script>
export default {
name: 'background',
data () {
return {
imgs: [],
isAnimate:false,
showImg: "static/bg/0.jpg",
showIndex: 0,
show: true
}
},
mounted:function(){
this.$nextTick(function () {
this.show=false;
this.bg_data();
});
},
methods:{
bg_data: function(){
var _this = this;
this.$http.get('static/data/bg.json').then(function(response){
_this.imgs = response.body;
});
},
beforeEnter: function (name) {
name.style.opacity=0;
name.style.transform = "scale(1) rotate(0deg)";
},
enter: function (name, done) {
var vm = this;
Velocity(name,
{ opacity: 1 ,
scale: 1.2,
rotateZ: "3deg"},
{
duration: 6000,
complete: function () {
done();
vm.show = false;
}
}
);
},
leave: function (name, done) {
var vm = this;
Velocity(name,
{ opacity: 0 ,
scale: 1,
rotateZ: "0deg"},
{
duration: 6000,
complete: function () {
done()
vm.showImg = vm.imgs[vm.showIndex==6 ? vm.showIndex=0 : vm.showIndex+=1 ].imgURL;
vm.show = true;
}
}
);
}
}
}
</script>
以上這篇vuejs 制作背景淡入淡出切換動(dòng)畫(huà)的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue金融數(shù)字格式化(并保留小數(shù))數(shù)字滾動(dòng)效果實(shí)現(xiàn)
這篇文章主要介紹了Vue金融數(shù)字格式化(并保留小數(shù)) 數(shù)字滾動(dòng)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
詳解mpvue scroll-view自動(dòng)回彈bug解決方案
設(shè)置了scroll-top的scroll-view組件,在組件所在vue實(shí)例data發(fā)生改變時(shí)會(huì)自動(dòng)回彈到最上方,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10
vue如何動(dòng)態(tài)設(shè)置背景漸變色
這篇文章主要介紹了vue如何動(dòng)態(tài)設(shè)置背景漸變色問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
vue-cli單頁(yè)面預(yù)渲染seo-prerender-spa-plugin操作
這篇文章主要介紹了vue-cli單頁(yè)面預(yù)渲染seo-prerender-spa-plugin操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
vue2.0+vuex+localStorage代辦事項(xiàng)應(yīng)用實(shí)現(xiàn)詳解
本篇文章給大家分享了一個(gè)用vue2.0+vuex+localStorage代辦事項(xiàng)應(yīng)用實(shí)現(xiàn)的代碼過(guò)程,有興趣的朋友跟著參考學(xué)習(xí)下。2018-05-05
使用proxytable 配置解決 vue-cli 的跨域請(qǐng)求問(wèn)題【推薦】
這篇文章主要介紹了利用 proxytable 配置解決 vue-cli 的跨域請(qǐng)求問(wèn)題,本文的目錄結(jié)構(gòu)基于 webpack 模板結(jié)構(gòu),需要的朋友可以參考下2018-05-05
詳解Vue+ElementUI從零開(kāi)始搭建自己的網(wǎng)站(一、環(huán)境搭建)
這篇文章主要介紹了Vue+ElementUI從零開(kāi)始搭建自己的網(wǎng)站(一、環(huán)境搭建),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
代替Vue?Cli的全新腳手架工具create?vue示例解析
這篇文章主要為大家介紹了代替Vue?Cli的全新腳手架工具create?vue示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
vue 3.x 中mixin封裝公用方法應(yīng)用方式
這篇文章主要介紹了vue 3.x 中mixin封裝公用方法應(yīng)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05

