解決vue.js中settimeout遇到的問(wèn)題(時(shí)間參數(shù)短效果不穩(wěn)定)
先看效果圖,這是彈窗效果,要求就是彈窗出現(xiàn)和消失時(shí)候不是很突兀,要有過(guò)渡效果。
首先看彈窗出現(xiàn)的實(shí)現(xiàn)思路,先加一個(gè)beforeActive類,再加一個(gè)active類。我們看審查元素,一開(kāi)始display:none;
在beforeActive中display:block;只是background: transparent;然后在一定時(shí)間后再加上active類。問(wèn)題就來(lái)了,在打開(kāi)彈窗代碼中,如下圖,settimeout第二個(gè)參數(shù)小于60ms效果就會(huì)不穩(wěn)定,有時(shí)候有過(guò)渡效果,有時(shí)候沒(méi)有過(guò)渡效果。
// openbtn(){ // let _this=this; // _this.show =true; // _this.isbeforeActive=true; // setTimeout(function(){ // _this.isactive=true; // },60) // },
想了很久也沒(méi)明白,后來(lái)終于明白,原來(lái)是vue渲染是有生命周期的,本來(lái)是先渲染befeactive,再渲染active,如果間隔時(shí)間太短就一次拿出來(lái)渲染了,所以沒(méi)有效果。
在退出彈框時(shí)候這個(gè)時(shí)間小于600ms就顯得特快,200ms的退出時(shí)間顯得比60ms的進(jìn)入還要急促很多,是因?yàn)橥顺龅葎?dòng)畫(huà)執(zhí)行完畢才可以,而一個(gè)動(dòng)畫(huà)的執(zhí)行需要300多,所以要用600ms
close_class(){ let _this=this; _this.isactive=false; setTimeout(function(){ _this.isbeforeActive=false; _this.show =false; },600) },
以下是完整代碼
<template> <div> <button @click="openbtn">顯示</button> <div v-show="show"> <div class="shenfenPop-page" v-bind:class="{beforeActive:isbeforeActive, active:isactive}" @click="cancel_all"> <div class="pop-wrap" @click.stop="stop"> <div class="pop-title">選擇您的身份 <div class="pop-sure" id="pop-sure" @click="decision_click">確定</div> <div class="pop-cancel" id="pop-cancel" @click="close_click">取消</div> </div> <div class="pop-list"> <ul> <li shenfen-id="jsptpl-style" v-for="(option,index) in options" @click.stop="add_class(index)" v-bind:class="{active:index==current}"> <div class="pop-info">{{option.text}}</div> <div class="pop-desc">{{option.value}}</div> <div class="pop-arrow"></div> </li> </ul> </div> </div> </div> </div> </div> </template> <script> export default { name: 'app', data() { return { show: false, current:1, isbeforeActive:false, isactive:false, options: [ {"text": "房東", "value": "房屋所有者,具備認(rèn)證房本資質(zhì)"}, {"text": "轉(zhuǎn)租", "value": "轉(zhuǎn)讓自己承租的房子"}, {"text": "經(jīng)紀(jì)人", "value": "房產(chǎn)中介,擁有專業(yè)的展示空間"}, {"text": "職業(yè)房東", "value": "公寓經(jīng)營(yíng)者/多房源管理者"} ], }; }, mounted() { }, computed: {}, methods: { add_class(index){ this.current=index; }, stop(){ }, openbtn(){ let _this=this; _this.show =true; _this.isbeforeActive=true; setTimeout(function(){ _this.isactive=true; },60) }, close_class(){ let _this=this; _this.isactive=false; setTimeout(function(){ _this.isbeforeActive=false; _this.show =false; },600) }, decision_click(){ this.close_class(); }, close_click() { this.close_class(); }, cancel_all(){ this.close_class(); }, }, watch: {}, }; </script> <style lang="scss" type="text/scss"> @import "../../../common/css/mixin"; * { margin: 0px; padding: 0px; list-style: none } .shenfenPop-page { width: 100%; height: 100%; position: fixed; top: 0px; transition: all 0.4s ease; } .shenfenPop-page .pop-wrap { transition: all 0.4s ease; position: absolute; width: 100%; bottom: 0px; background: #ffffff; } .shenfenPop-page .pop-title { height: 1.2rem; background: #f9fafc; text-align: center; font-size: 0.37333rem; color: #999999; line-height: 1.2rem; position: relative; } .shenfenPop-page .pop-title:after { content: ""; position: absolute; top: 0; left: 0; border: 1px solid #e3e3e4; -webkit-box-sizing: border-box; box-sizing: border-box; width: 200%; height: 200%; -webkit-transform: scale(0.5); transform: scale(0.5); -webkit-transform-origin: left top; transform-origin: left top; } .shenfenPop-page .pop-title .pop-sure, .shenfenPop-page .pop-title .pop-cancel { position: absolute; z-index: 1; width: 1.6rem; height: 1.2rem; line-height: 1.22667rem; color: #ff552e; top: 0px; right: 0px; } .shenfenPop-page .pop-title .pop-sure.pop-cancel, .shenfenPop-page .pop-title .pop-cancel.pop-cancel { right: auto; left: 0px; color: #7b7b7b; } .shenfenPop-page .pop-list { widtH: 100%; } .shenfenPop-page .pop-list ul { width: 9.33333rem; margin: 0 auto; } .shenfenPop-page .pop-list ul li { width: 100%; height: 1.86667rem; position: relative; } .shenfenPop-page .pop-list ul li:after { border-radius: 0px; content: ""; position: absolute; top: 0; left: 0; z-index: -1; border-bottom: 1px solid #f1f1f1; -webkit-box-sizing: border-box; box-sizing: border-box; width: 200%; height: 200%; -webkit-transform: scale(0.5); transform: scale(0.5); -webkit-transform-origin: left top; transform-origin: left top; } .shenfenPop-page .pop-list ul li .pop-info { position: absolute; top: 0.4rem; font-size: 0.45333rem; color: #333333; left: 0.06667rem; } .shenfenPop-page .pop-list ul li .pop-desc { font-size: 0.32rem; position: absolute; left: 0.06667rem; top: 0.98667rem; color: #7b7b7b; } .shenfenPop-page .pop-list ul li .pop-arrow { position: absolute; right: 0.26667rem; width: 22px; height: 22px; top: 50%; margin-top: -11px; background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAASFBMVEUAAADHx87IyM7IyM7Kys/IyM7Jyc/IyNHKytHW1tb////Hx87IyM3IyM3IyM7IyM3Jyc7IyM7Jyc7Ly9HIyNPHx83IyM3Hx81MCBRBAAAAF3RSTlMA8qt/QttXOCQMAennw7+ZjYNtLBf2kCIzlTUAAAEGSURBVDjLjJFbbsMwDAQpUpIl+R07mfvftEBdFE7ih+ZzMSB2QfmgNLMOwbkw6NwUucKPHTu60Z+qTQ+EKfqUc/JxCkDfHKpJobVlHy3WgqaDBg5n+TPN5nBfXR6gqxywKjzeoxfYyfZi8Hq/+4xySnzub3uIckGE/97JYXKJ4ZJsKFqu5aLo3y9wq9ywOrbv9JjcYvTbujbfy7n93ThiUoExipSOpUZe6Io0BKki0MjMVCdPzKLEOjmiMuDrZM8ggVQnJ4I4cp2ccT+V1TkBAAAIxDD/rqMhOwPP0VZxtVED1urqKHXuCtJEtMI/b1UPOygoyAy+BoyD3IH5aGIE1Gp7aZaOAVpkFMUoYaaBAAAAAElFTkSuQmCC"); background-size: cover; } .shenfenPop-page .pop-list ul li.active .pop-arrow { background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAAPFBMVEUAAAD/VS7/VjH/VzL/VS7/Vi//VS7/VS//VS//VS7/VS7/Vi//Vy//WjX/VS7/VS7/Yz//4tv/////m4Tfv1CvAAAAD3RSTlMA8UQ459nDv62lmYNtIpDE1s/TAAAA6klEQVQ4y53VS5KDMAwEUPmP7ZAmk/vfdRaZcoKITDO95ZWqjGVJdEKvJTmXSu1BpvEt4iOxeZOGjEPy9/Lrgq9Z1qP1Dkac1/aGSW57e8c0d7auru1xGj/+gzvHbv3DC4gsLxtA5XU7mcP5/HTbc3fGNrWPn6GbiMS5fWyjB0UCa4EgnbboUmmLKoW2KJJoiySOtnAf+Llpe8Bp2PHZsEhSFLAtilQobVpU6VDatOgSoPTO6o6OSps2jhZ9a8uijeZ/a2XVA897bdl8/cHyo4AfMtfHFz8Y+ZHLD/Ora4JfQPxq+//S5NfxLzPrWEC1LMpOAAAAAElFTkSuQmCC"); } .shenfenPop-page.beforeActive { display: block; background: transparent; } .shenfenPop-page.beforeActive .pop-wrap { transform: translateY(100%); } .shenfenPop-page.active { /*background: rgba(0, 0, 0, 0.7);*/ } .shenfenPop-page.active .pop-wrap { transform: translateY(0); } </style>
補(bǔ)充知識(shí):setTimeout在vue中的正確使用
如下所示:
mounted(){ setTimeout(this.tishi(),5000) },
這樣寫(xiě),發(fā)現(xiàn)直接就執(zhí)行了tishi函數(shù),而不是5s之后執(zhí)行
修改為:
mounted(){ let _this = this; setTimeout(() => { _this.tishi() },5000) },
以上這篇解決vue.js中settimeout遇到的問(wèn)題(時(shí)間參數(shù)短效果不穩(wěn)定)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
在vue中實(shí)現(xiàn)echarts隨窗體變化
這篇文章主要介紹了在vue中實(shí)現(xiàn)echarts隨窗體變化,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07前端vue框架select下拉數(shù)據(jù)量過(guò)大造成卡頓問(wèn)題解決辦法
這篇文章主要給大家介紹了關(guān)于前端vue框架select下拉數(shù)據(jù)量過(guò)大造成卡頓問(wèn)題解決辦法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用select具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07vue插槽slot的簡(jiǎn)單理解與用法實(shí)例分析
這篇文章主要介紹了vue插槽slot的簡(jiǎn)單理解與用法,結(jié)合實(shí)例形式分析了vue插槽slot的功能、原理、相關(guān)使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03vue內(nèi)置組件keep-alive事件動(dòng)態(tài)緩存實(shí)例
這篇文章主要介紹了vue內(nèi)置組件keep-alive事件動(dòng)態(tài)緩存實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10vue 項(xiàng)目中實(shí)現(xiàn)按鈕防抖方法
這篇文章主要介紹了vue 項(xiàng)目中實(shí)現(xiàn)按鈕防抖方法,首先需要新建 .js文件存放防抖方法,引入防抖文件,methods中添加方法,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12vue項(xiàng)目接入高德地圖點(diǎn)擊地圖獲取經(jīng)緯度以及省市區(qū)功能
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目接入高德地圖點(diǎn)擊地圖獲取經(jīng)緯度以及省市區(qū)功能的相關(guān)資料,開(kāi)發(fā)中我們需要地圖定位,就是用戶輸入位置,自動(dòng)定位獲取經(jīng)緯度,需要的朋友可以參考下2023-08-08理解Vue2.x和Vue3.x自定義指令用法及鉤子函數(shù)原理
這篇文章主要介紹了理解Vue2.x和Vue3.x的自定義指令的用法及鉤子函數(shù)原理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-09-09Vue2遞歸組件實(shí)現(xiàn)樹(shù)形菜單
這篇文章主要為大家詳細(xì)介紹了Vue2遞歸組件實(shí)現(xiàn)樹(shù)形菜單的相關(guān)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04