需求:当页面产生滚动,位置超过侧边栏的 顶部阈值后,侧边栏变为固定定位。
解决方法:使用position: sticky 粘性定位
刚开始的想法是获取滚动距离,动态添加position: fixed,但是会出现动态添加fixed会导致宽度位置发生变化。最后选择了position: sticky
<div class="content-right">
<div class="right-fixed">
<div v-for="(item, index) in data.hotList" :key="index" class="content-right-item" @click="rightLink(item)">
<div class="num">{
{ index + 1 }}</div>
<div class="right-title">
{
{ item.collegeTitle }}
</div>
</div>
</div>
</div>
.right-fixed {
position: -webkit-sticky; /* 对于 Safari 的兼容性 */
position: sticky;
right: 0;
top: 0;
}