欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 文字滚动通知功能实现 vue 组件

文字滚动通知功能实现 vue 组件

2024/10/24 23:24:51 来源:https://blog.csdn.net/zfz5720/article/details/141431347  浏览:    关键词:文字滚动通知功能实现 vue 组件

工作中有使用到文字滚动功能,做个demo记录,方便下次复制,分享给大家。

点击 预览
https://works-ipo66c6f46dc36334645e6fc471-5173.node01.inscode.run/

<script setup lang="ts">
import { ref, nextTick } from 'vue';
const noticeList = ref([]);const getNoticeList = async () => {const list = ['测试文字','11111111111111111111111111111111111111111111111111111111','哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈',];noticeList.value = list;nextTick(() => {const content = document.querySelector('.scroll-content');const contentOffsetWidth = content.offsetWidth;// 根据内容长度计算动画持续时间// 假设每秒滚动100px width 越大,速度越快const width = 100;const duration = Math.floor(contentOffsetWidth / width);content.style.animationDuration = `${duration}s`;});
};getNoticeList();
</script><template><div class="scroll-container"><div class="scroll-content"><div v-for="(item, index) in noticeList" :key="item + index" class="item">{{ item }}</div></div></div>
</template><style scoped>
.scroll-container {display: flex;align-items: center;background: red;width: 800px;overflow-x: hidden; /* 启用水平滚动 */overflow-y: hidden; /* 隐藏垂直滚动条 */white-space: nowrap; /* 防止内容换行 */position: relative;z-index: 99;
}.scroll-content {display: inline-block; /* 让内容作为内联块级元素,以便宽度自动调整 */padding: 10px; /* 可选:添加内边距以调整内容位置 */will-change: auto;animation: scroll-left 10s linear infinite; /* 动画设置 */
}.item {display: inline-block; /* 让每个条目水平排列 */text-align: left;padding-left: 800px;color: #fff;
}@keyframes scroll-left {from {transform: translateX(0);}to {transform: translateX(-100%); /* 根据内容总长度调整 */}
}
</style>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com