Skip to content

Commit 3bca6d9

Browse files
committed
Revert "feat: 图片占位方式更新,采用 css 原生属性,不再使用 js"
This reverts commit f46c352.
1 parent dec6ada commit 3bca6d9

File tree

7 files changed

+122
-30
lines changed

7 files changed

+122
-30
lines changed

src/pages/afterglow/AfterglowIndex.vue

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import CategorySecond from '../../components/CategorySecond.vue';
44
import AppH2 from '../../components/AppH2.vue';
55
import { useRoute } from 'vue-router';
66
import highlightElement from '../../utils/animation';
7+
import { useHighBrightnessControllerStore } from '../../stores/miscellaneous';
78
import AppSupport from '../../components/AppSupport.vue';
89
import AccordionNavigation from '../../components/AccordionNavigation.vue';
9-
import { useHighlightWatch } from '../../utils/utils';
10+
import { onImgLoad, useHighlightWatch, useSeizeSeat } from '../../utils/utils';
1011
1112
const route = useRoute();
1213
@@ -55,6 +56,8 @@ const docList = reactive([
5556
path: '/afterglow/isa'
5657
}
5758
]);
59+
60+
const [observer, imgHeights] = useSeizeSeat('bgImg', 1.3333);
5861
</script>
5962

6063
<template>
@@ -69,11 +72,11 @@ const docList = reactive([
6972
<div>
7073
<AccordionNavigation :navigation-list="navigationList" />
7174
</div>
72-
<div ref="bgImg">
75+
<div ref="bgImg" class="bg-img-height">
7376
<img
74-
77+
@load="onImgLoad(observer, imgHeights[0])"
7578
src="/assets/afterglow/afterglow.zh-cn.jpg"
76-
class="w-full imgScale1 mt-2"
79+
class="w-full h-auto mt-2"
7780
alt="" />
7881
</div>
7982
</div>
@@ -105,8 +108,8 @@ const docList = reactive([
105108
</template>
106109

107110
<style scoped>
108-
.imgScale1 {
109-
aspect-ratio: 4/3;
111+
.bg-img-height {
112+
height: v-bind('imgHeights[0].value');
110113
}
111114
</style>
112115
./components/Header.vue

src/pages/aosc-os/AoscIndex.vue

+8-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useRoute } from 'vue-router';
66
import highlightElement from '../../utils/animation';
77
import AppSupport from '../../components/AppSupport.vue';
88
import AccordionNavigation from '../../components/AccordionNavigation.vue';
9-
import { useHighlightWatch } from '../../utils/utils';
9+
import { onImgLoad, useHighlightWatch, useSeizeSeat } from '../../utils/utils';
1010
1111
const route = useRoute();
1212
@@ -64,6 +64,8 @@ const docList = reactive([
6464
url: '/'
6565
} */
6666
]);
67+
68+
const [observer, imgHeights] = useSeizeSeat('bgImg', 1.6006);
6769
</script>
6870

6971
<template>
@@ -79,10 +81,11 @@ const docList = reactive([
7981
<div>
8082
<AccordionNavigation :navigation-list="navigationList" />
8183
</div>
82-
<div ref="bgImg">
84+
<div ref="bgImg" class="bg-img-height">
8385
<img
86+
@load="onImgLoad(observer, imgHeights[0])"
8487
src="/assets/aosc-os/aosc-os.zh-cn.jpg"
85-
class="w-full imgScale1 mt-2"
88+
class="w-full h-auto mt-2"
8689
alt="" />
8790
</div>
8891
</div>
@@ -129,8 +132,8 @@ const docList = reactive([
129132
</template>
130133

131134
<style scoped>
132-
.imgScale1 {
133-
aspect-ratio: 8/5;
135+
.bg-img-height {
136+
height: v-bind('imgHeights[0].value');
134137
}
135138
</style>
136139
./components/Header.vue

src/pages/index/components/ContentMain.vue

+13-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { reactive, ref } from 'vue';
33
import CategorySecond from '/src/components/CategorySecond.vue';
44
import NewsCategoryList from '/src/pages/news/components/NewsCategoryList.vue';
5-
import { requestGetJson } from '../../../utils/utils';
5+
import { onImgLoad, requestGetJson, useSeizeSeat } from '../../../utils/utils';
66
import { ElIcon } from 'element-plus';
77
88
/**
@@ -50,14 +50,20 @@ const zhuanlanList = reactive([
5050
'我社一向以友好负责的支持工作著称,尊重用户的时间与精力是我们的工作准则。社区也是信息共享的强大后盾——在各社区聊天群组,我们时刻准备着为您排忧解难。'
5151
}
5252
]);
53+
54+
const [ observer, imgHeights ] = useSeizeSeat('bgImg', 1.4545);
5355
</script>
5456

5557
<template>
5658
<div class="w-[62.5%] bg-content-main-bg p-0">
57-
<app-link
59+
<a
60+
ref="bgImg"
5861
href="/download#aosc-os-download"
59-
class="w-full flex">
60-
<el-image class="w-full imgScale1" src="/assets/jumbotron/main1.svg">
62+
class="bg-img-height w-full flex">
63+
<el-image
64+
@load="onImgLoad(observer, imgHeights[0])"
65+
class="w-full"
66+
src="/assets/jumbotron/main1.svg">
6167
<template #error>
6268
<div class="image-slot cursor-pointer">
6369
<el-icon>
@@ -66,7 +72,7 @@ const zhuanlanList = reactive([
6672
</div>
6773
</template>
6874
</el-image>
69-
</app-link>
75+
</a>
7076
<!-- 资讯要点 -->
7177
<div>
7278
<category-second title="资讯要点" />
@@ -114,8 +120,8 @@ const zhuanlanList = reactive([
114120
</template>
115121

116122
<style scoped>
117-
.imgScale1 {
118-
aspect-ratio: 16/11;
123+
.bg-img-height {
124+
height: v-bind('imgHeights[0].value');
119125
}
120126
</style>
121127
../../../components/CategorySecond.vue

src/pages/index/components/RightBar.vue

+13-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { reactive } from 'vue';
33
import CategorySecond from '/src/components/CategorySecond.vue';
44
import AppLink from '../../../components/AppLink.vue';
5+
import { onImgLoad, useSeizeSeat } from '../../../utils/utils';
56
67
const distroList = reactive([
78
{
@@ -57,20 +58,25 @@ const commonLinkList = reactive([
5758
title: '赞助硬件或服务'
5859
}
5960
]);
61+
62+
const [observer1, buffer] = useSeizeSeat('bgImg1', 1.7455);
63+
const [observer2, imgHeights] = useSeizeSeat('bgImg2', 1.7455, buffer);
6064
</script>
6165

6266
<template>
6367
<div class="bg-right-bar-bg w-[37.5%] flex flex-col">
6468
<a href="/aosc-os/right-for-me" ref="bgImg1" class="bg-img-height1">
6569
<img
70+
@load="onImgLoad(observer1, imgHeights[0])"
6671
ref="bgImg"
6772
src="/assets/jumbotron/minor1.svg"
68-
class="w-full cursor-pointer imgScale1" />
73+
class="w-full cursor-pointer" />
6974
</a>
7075
<a href="https://bbs.aosc.io/" ref="bgImg2" class="bg-img-height2">
7176
<img
77+
@load="onImgLoad(observer2, imgHeights[1])"
7278
src="/assets/jumbotron/minor2.svg"
73-
class="w-full cursor-pointer imgScale1"
79+
class="w-full cursor-pointer"
7480
href="https://bbs.aosc.io/" />
7581
</a>
7682
<div class="flex flex-col flex-grow">
@@ -129,7 +135,10 @@ const commonLinkList = reactive([
129135
</template>
130136

131137
<style scoped>
132-
.imgScale1 {
133-
aspect-ratio: 96/55;
138+
.bg-img-height1 {
139+
height: v-bind('imgHeights[0].value');
140+
}
141+
.bg-img-height2 {
142+
height: v-bind('imgHeights[1].value');
134143
}
135144
</style>

src/pages/liblol/LiblolIndex.vue

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { reactive } from 'vue';
33
import CategorySecond from '../../components/CategorySecond.vue';
44
import AppSupport from '../../components/AppSupport.vue';
55
import AccordionNavigation from '../../components/AccordionNavigation.vue';
6+
import { onImgLoad, useSeizeSeat } from '../../utils/utils';
67
78
const navigationList = [
89
{
@@ -49,6 +50,8 @@ const docList = reactive([
4950
url: 'https://liblol.aosc.io/docs/dev/design/'
5051
}
5152
]);
53+
54+
const [observer, imgHeights] = useSeizeSeat('bgImg', 2.8);
5255
</script>
5356

5457
<template>
@@ -66,10 +69,13 @@ const docList = reactive([
6669
<div>
6770
<AccordionNavigation :navigation-list="navigationList" />
6871
</div>
69-
<div ref="bgImg" class="bg-img-height">
72+
<div
73+
ref="bgImg"
74+
class="bg-img-height">
7075
<img
76+
@load="onImgLoad(observer, imgHeights[0])"
7177
src="/assets/liblol/liblol.svg"
72-
class="w-full mt-2 imgScale1"
78+
class="w-full h-auto mt-2"
7379
alt="" />
7480
</div>
7581
</div>
@@ -79,8 +85,8 @@ const docList = reactive([
7985
</template>
8086

8187
<style scoped>
82-
.imgScale1 {
83-
aspect-ratio: 14/5;
88+
.bg-img-height {
89+
height: v-bind('imgHeights[0].value');
8490
}
8591
</style>
8692
./components/Header.vue

src/pages/oma/OmaIndex.vue

+11-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { reactive } from 'vue';
33
import CategorySecond from '/src/components/CategorySecond.vue';
44
import AppLink from '../../components/AppLink.vue';
55
import AccordionNavigation from '../../components/AccordionNavigation.vue';
6+
import { onImgLoad, useSeizeSeat } from '../../utils/utils';
67
const navigationList = [
78
{
89
title: '代码仓库',
@@ -33,6 +34,8 @@ const _docList = reactive([
3334
url: '#'
3435
}
3536
]);
37+
38+
const [observer, imgHeights] = useSeizeSeat('bgImg', 1.5167);
3639
</script>
3740

3841
<template>
@@ -78,8 +81,12 @@ const _docList = reactive([
7881
<div>
7982
<AccordionNavigation :navigation-list="navigationList" />
8083
</div>
81-
<div ref="bgImg">
82-
<img src="/assets/oma/oma.png" class="w-full imgScale1 mt-2" alt=""
84+
<div ref="bgImg" class="bg-img-height">
85+
<img
86+
@load="onImgLoad(observer, imgHeights[0])"
87+
src="/assets/oma/oma.png"
88+
class="w-full h-auto mt-2"
89+
alt=""
8390
/></div>
8491
</div>
8592

@@ -90,8 +97,8 @@ const _docList = reactive([
9097
</template>
9198

9299
<style scoped>
93-
.imgScale1 {
94-
aspect-ratio: 3/2; /* 91/60 近似值 */
100+
.bg-img-height {
101+
height: v-bind('imgHeights[0].value');
95102
}
96103
</style>
97104
./components/Header.vue

src/utils/utils.js

+58
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,64 @@ export const BToMB = (byteSize, fixed = 3) => {
124124
return (byteSize / 1024 / 1024).toFixed(fixed);
125125
};
126126

127+
export const deObserver = (observers) => {
128+
if (Array.isArray(observers)) {
129+
observers.forEach((observer) => {
130+
observer.disconnect();
131+
});
132+
} else if (observers) {
133+
observers.disconnect();
134+
}
135+
};
136+
137+
export const imgPreOccupiedSpace = (
138+
anchorImg,
139+
imgHeight,
140+
proportion,
141+
fixedHeight
142+
) => {
143+
const observer = new ResizeObserver(() => {
144+
imgHeight.value = fixedHeight
145+
? fixedHeight
146+
: (anchorImg.value.clientWidth / proportion).toFixed(2) + 'px';
147+
});
148+
observer.observe(anchorImg.value);
149+
return observer;
150+
};
151+
152+
export const onImgLoad = (observers, imgHeight) => {
153+
deObserver(toValue(observers));
154+
imgHeight.value = 'auto';
155+
};
156+
157+
export const useSeizeSeat = (refName, proportion, imgHeights, fixedHeight) => {
158+
const newHeights = ref(0);
159+
if (imgHeights !== undefined) {
160+
imgHeights.value.push(newHeights);
161+
} else {
162+
imgHeights = shallowRef([newHeights]);
163+
}
164+
const img = useTemplateRef(refName);
165+
166+
// 此处异步执行,如果不使用ref包裹返回的observer为null
167+
let observer = ref();
168+
169+
onMounted(() => {
170+
observer.value = imgPreOccupiedSpace(
171+
img,
172+
newHeights,
173+
proportion,
174+
fixedHeight
175+
);
176+
});
177+
178+
onUnmounted(() => {
179+
// 在组件销毁前取消观察
180+
deObserver(observer.value);
181+
});
182+
return [observer, imgHeights];
183+
};
184+
127185
export const useHighlightWatch = (switchHash) => {
128186
const highBrightnessControllerStore = useHighBrightnessControllerStore();
129187
const route = useRoute();

0 commit comments

Comments
 (0)