NoticeModal.vue 4.18 KB
Newer Older
zhangsan's avatar
1  
zhangsan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<template>
  <transition name="fade">
    <div v-if="visible" class="notice-modal-overlay" @click.self="handleClose">
      <transition :name="transitionName" mode="out-in" @before-leave="beforeLeave" @after-leave="afterLeave">
        <div v-if="visible" class="notice-modal" :key="currentIndex">
          <img src="@/static/home/logo.png" alt="" class="notice-logo" />
          <div class="notice-content">
            <!-- <div class="notice-close" @click="handleClose">
              <van-icon name="close" size="24" color="#666" />
            </div> -->
            <div class="notice-body">
              <slot :index="currentIndex">
                <div v-html="noticeList[currentIndex]?.noticeContent"></div>
              </slot>
            </div>
          </div>
          <div class="notice-footer">
zhangsan's avatar
1  
zhangsan committed
18
            <div class="notice-button" @click="handleAction">
zhangsan's avatar
1  
zhangsan committed
19
              {{ isLastPage ? '我知道了' : '下一页' }}
zhangsan's avatar
1  
zhangsan committed
20
            </div>
zhangsan's avatar
1  
zhangsan committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
          </div>
        </div>
      </transition>
    </div>
  </transition>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'

interface NoticeItem {
  noticeBody: string
}
const visible = ref(false)
interface Props {
  noticeList: NoticeItem[]
}
const props = defineProps<Props>()
const currentIndex = ref(0)
const slideDirection = ref('next')
const transitionName = computed(() => `slide-${slideDirection.value}`)

const isLastPage = computed(() => currentIndex.value === props.noticeList.length - 1)
console.log(props.noticeList)
const handleAction = () => {
  if (isLastPage.value) {
    handleClose()
  } else {
    slideDirection.value = 'next'
    currentIndex.value++
  }
}

const handleClose = () => {
  visible.value = false
  // 重置状态
  setTimeout(() => {
    currentIndex.value = 0
    slideDirection.value = 'next'
  }, 300)
}

const beforeLeave = () => {
  document.body.style.overflow = 'hidden'
}

const afterLeave = () => {
  document.body.style.overflow = ''
}

// 暴露打开方法
const open = () => {
  visible.value = true
}

defineExpose({ open })
</script>

<style lang="scss" scoped>
.notice-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
zhangsan's avatar
1  
zhangsan committed
92

zhangsan's avatar
1  
zhangsan committed
93 94 95 96
.notice-logo {
  width: 150px;
  margin-top: 60px;
}
zhangsan's avatar
1  
zhangsan committed
97

zhangsan's avatar
1  
zhangsan committed
98 99 100 101 102 103
.notice-modal {
  width: 100%;
  background: url('@/static/other/noticebg.png') no-repeat;
  background-size: 100% 100%;
  border-radius: 8px;
  height: 80vh;
zhangsan's avatar
1  
zhangsan committed
104
  padding: 0 30px;
zhangsan's avatar
1  
zhangsan committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.notice-content {
  position: relative;
  padding: 20px 15px;
  flex: 1;
  min-height: 40vh;
  max-height: 70vh;
  overflow-y: auto;
}

.notice-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 1;
}

.notice-body {
  height: 100%;
  overflow-y: auto;
  padding: 15px 10px;
  color: #333;
  font-size: 14px;
  line-height: 1.5;

  // 允许v-html内容继承样式
  :deep(*) {
    max-width: 100%;
    height: auto;
  }

  // 自定义滚动条样式
  &::-webkit-scrollbar {
    width: 4px;
  }

  &::-webkit-scrollbar-thumb {
    background: #ddd;
    border-radius: 2px;
  }
}

.notice-button {
  width: 100%;
  height: 40px;
zhangsan's avatar
1  
zhangsan committed
161 162 163 164
  line-height: 40px;
  text-align: center;
  color: red;
  // background: linear-gradient(to right, #ff4d4f, #ff7875);
zhangsan's avatar
1  
zhangsan committed
165 166
  border: none;
  border-radius: 20px;
zhangsan's avatar
1  
zhangsan committed
167 168
  font-size: 20px;
  font-weight: bold;
zhangsan's avatar
1  
zhangsan committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  cursor: pointer;
  transition: opacity 0.3s;

  &:active {
    opacity: 0.8;
  }
}

// 淡入淡出动画
.fade-enter-active,
.fade-leave-active {
  transition: opacity 1s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

// 滑动动画
.slide-next-enter-active,
.slide-next-leave-active,
.slide-prev-enter-active,
.slide-prev-leave-active {
  transition: all 1s ease;
}

.slide-next-enter-from {
  transform: translateX(100%);
  opacity: 0;
}

.slide-next-leave-to {
  transform: translateX(-100%);
  opacity: 0;
}

.slide-prev-enter-from {
  transform: translateX(-100%);
  opacity: 0;
}

.slide-prev-leave-to {
  transform: translateX(100%);
  opacity: 0;
}
</style>