您好,登錄后才能下訂單哦!
本篇內容主要講解“Vue怎么實現星空效果”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Vue怎么實現星空效果”吧!
星空效果如下
1.星空背景子組件
<template> <div class="stars"> <div class="star" v-for="(item, index) in starsCount" :key="index" ref="star"></div> </div> </template> <script> export default { name: 'StarBackground', props: {}, data() { return { starsCount: 1200, distance: 800 } }, mounted() { this.initStars() }, methods: { initStars() { let starArr = this.$refs.star starArr.forEach(item => { let speed = 0.2 + (Math.random() * 1) let thisDistance = this.distance + (Math.random() * 300) item.style.transformOrigin = `0 0 ${thisDistance}px` item.style.transform = `translate3d(0, 0, -${thisDistance}px) rotateY(${(Math.random() * 360)}deg) rotateX(${(Math.random() * -50)}deg) scale(${speed}, ${speed})` }) } } } </script> <style scoped lang="scss"> @keyframes rotate { 0% { transform: perspective(600px) rotateZ(20deg) rotateX(-40deg) rotateY(0); } 100% { transform: perspective(600px) rotateZ(20deg) rotateX(-40deg) rotateY(-360deg); } } .stars { transform: perspective(500px); transform-style: preserve-3d; position: absolute; perspective-origin: 50% 100%; left: 50%; animation: rotate 90s infinite linear; bottom: -200px; } .star { width: 2px; height: 2px; background: #f7f7b8; position: absolute; top: 0; left: 0; backface-visibility: hidden; } </style>
2.登錄頁引用子組件
<template> <div class="login-container"> <star-background /> </div> </template> <script> import StarBackground from './components/StarBackground' export default { beforeCreate: function() { document.getElementsByTagName('body')[0].className = 'body-bg' }, components: { StarBackground } } </script> <style lang="scss"> .body-bg { background-attachment: fixed; overflow: hidden; } .login-container { height: 100%; width: 100%; overflow: hidden; background-color: #050608; } </style>
background-attachment: fixed;
很重要,需要把界面固定住,不然下拉會出現空白
到此,相信大家對“Vue怎么實現星空效果”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。