File tree Expand file tree Collapse file tree 3 files changed +105
-2
lines changed
Expand file tree Collapse file tree 3 files changed +105
-2
lines changed Original file line number Diff line number Diff line change 1+ <!-- 在vitepress右侧的目录导航中插入动态广告组件-->
2+
3+ <script setup>
4+ import { ref , onMounted , onUnmounted } from ' vue'
5+
6+ const ads = ref ([])
7+ const currentAdIndex = ref (0 )
8+ let intervalId = null
9+
10+ const fetchAds = async () => {
11+ return [
12+ {
13+ id: 1 ,
14+ imageUrl: ' https://mangoproxy.com/assetsfile/images/logomango.webp' ,
15+ landingUrl: ' https://mangoproxy.com/?utm_source=mediacrawler&utm_medium=repository&utm_campaign=default' ,
16+ text: ' 全局IP代理白金推荐,支持210+国家'
17+ }
18+ ]
19+ }
20+
21+ const nextAd = () => {
22+ currentAdIndex .value = (currentAdIndex .value + 1 ) % ads .value .length
23+ }
24+
25+ onMounted (async () => {
26+ ads .value = await fetchAds ()
27+ intervalId = setInterval (nextAd, 3000 )
28+ })
29+
30+ onUnmounted (() => {
31+ if (intervalId) clearInterval (intervalId)
32+ })
33+ </script >
34+
35+ <template >
36+ <div class =" vp-ad-carousel" >
37+ <template v-if =" ads .length > 0 " >
38+ <div class =" ad-content" >
39+ <a :href =" ads[currentAdIndex].landingUrl" target =" _blank" rel =" noopener noreferrer" >
40+ <img :src =" ads[currentAdIndex].imageUrl" :alt =" ads[currentAdIndex].text" class =" ad-image" >
41+ <p class =" ad-text" >{{ ads[currentAdIndex].text }}</p >
42+ </a >
43+ </div >
44+ </template >
45+ <p v-else class =" loading" >Loading ads...</p >
46+ </div >
47+ </template >
48+
49+ <style scoped>
50+ .vp-ad-carousel {
51+ margin-top : 1rem ;
52+ padding : 1rem ;
53+ background-color : var (--vp-c-bg-soft );
54+ border-radius : 8px ;
55+ font-size : 0.875rem ;
56+ line-height : 1.5 ;
57+ }
58+
59+ .ad-content {
60+ display : flex ;
61+ flex-direction : column ;
62+ align-items : center ;
63+ }
64+
65+ .ad-image {
66+ max-width : 130px ;
67+ height : auto ;
68+ margin-bottom : 0.5rem ;
69+ }
70+
71+ .ad-text {
72+ text-align : center ;
73+ color : var (--vp-c-text-1 );
74+ }
75+
76+ .loading {
77+ text-align : center ;
78+ color : var (--vp-c-text-2 );
79+ }
80+
81+ a {
82+ text-decoration : none ;
83+ color : inherit ;
84+ }
85+ </style >
Original file line number Diff line number Diff line change 1+ <!-- .vitepress/theme/MyLayout.vue-->
2+ <script setup>
3+ import DefaultTheme from ' vitepress/theme'
4+ import DynamicAds from ' ./DynamicAds.vue'
5+ const { Layout } = DefaultTheme
6+ </script >
7+
8+ <template >
9+ <Layout >
10+ <template #aside-bottom >
11+ <DynamicAds />
12+ </template >
13+ </Layout >
14+ </template >
Original file line number Diff line number Diff line change 11// .vitepress/theme/index.js
22import DefaultTheme from 'vitepress/theme'
3- import './custom.css '
3+ import MyLayout from './MyLayout.vue '
44
5- export default DefaultTheme
5+ export default {
6+ extends : DefaultTheme ,
7+ // 使用注入插槽的包装组件覆盖 Layout
8+ Layout : MyLayout
9+ }
You can’t perform that action at this time.
0 commit comments