Vue -- 只彈一次的彈框


核心代碼是 getCookie()部分,控制彈框的顯示隱藏則在 created()中。


<template>
  <div v-if="isShow"> <!--最外層背景-->
    <div class="popup_container"> <!--居中的容器-->
      <img @click="noPopup" src="delete.png" alt=""> <!--關閉彈框-->
      <div class="popup_text"> <!--內容部分-->
       Lorem ipsum dolor sit amet, consectetur adipisicing elit. At, atque ea eveniet laudantium magni, maiores nam nihil non numquam odio pariatur perferendis placeat quas quasi sit soluta, sunt ullam voluptatibus.    
      </div>
    </div>
  </div>
</template>
<script>
  export default {
    data(){
      return{
        isShow: true,
      }
    },
    created(){
      if (this.getCookie('popped') == ''){ //cookie 中沒有 popped 則賦給他一個值(此時彈框顯示)
        document.cookie = "popped = yes";
      }else{
        this.isShow = false; //若cookie 中已經有 popped 值,則彈框再不會顯示
      }
    },
    methods: {
      noPopup(){
        this.isShow = false;
      },
      getCookie(Name) { //cookie
        var search = Name + "=";
        var returnValue = "";
        if (document.cookie.length > 0) {
          var offset = document.cookie.indexOf(search);
          if (offset !== -1) {
            offset += search.length;
            var end = document.cookie.indexOf(";", offset);
            if (end == -1){
              end = document.cookie.length;
            }
            returnValue = decodeURIComponent(document.cookie.substring(offset, end));
          }
        }
        return returnValue;
      },
    },
  }
</script>
<style scoped>
    /*樣式部分*/
</style>

原文地址:https://segmentfault.com/a/1190000013019136


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM