使用sass来画星空效果

相信大家能够在网上搜到各种效果的星空背景,写的方法也是各种各样。这里给大家分享一个用sass来写的星空效果。(星星是会动的,这里就不用git动图了,最后的效果以及源码放在末尾)

 首先我们看到图片中星星有大有小,所以我们的星星应该有多层。这里三个div就代表三层星星,只要一层写好了,那么其他两次也是改一下大小复制一下就可以了。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div class="layer1"></div>
    <div class="layer2"></div>
    <div class="layer3"></div>
</body>
<style>
@import url(./index.css);
</style>
</html>

开始写layer1的星星,星星就是小圆点。我这里通过box-shadow这个属性来生成,应为他可以写多个阴影。box-shadow

.layer1{
    position: fixed;
    width: $size;
    height: $size;
    top: 0;
    left: 0;
    border-radius: $size;
    background-color: white;
    box-shadow: 10px 10px #fff, 15px 15px #fff;
}

最重要的是如果去写一个函数去生成一个我们想要的阴影,这里用到了 unquote 去掉字符串的引号,在scss中类似模板字符串的 #{ 变量} ,还有就是为什么for循环从2开始,因为我们先声明$shadow的时候已经给了第一个点的值了。还有scss中取随机数的函数 random 一定要注意字符串的空格,不然运行出来css识别不了。

@function getShow($n){
    $shadow: unquote('#{random(100)}vw') unquote('#{random(100)}vh') #fff;
    @for $i from 2 through $n {
        $shadow: '#{$shadow}' + ',' + '#{unquote('#{random(100)}vw') unquote('#{random(100)}vh') #fff}'
    }
    

    @return unquote($string: $shadow)
}

 写完过后我们再给一个向上的动画,让星星动起来。

@keyframes moveUp {
    100% {
        transform: translateY(-100vh);
    }
}

这里有个问题就是动画一直是循环播放的,网上其他大部份的星空就是在这里有个问题,页面会卡顿一下。看起来不连贯。所以这里需要在生成一个利用伪元素生成一个元素放在它的下面,当他向上平移的时候下面的页面也会平移。看起来就非常连贯且丝滑。

.layer1{
    position: fixed;
    width: $size;
    height: $size;
    top: 0;
    left: 0;
    border-radius: $size;
    background-color: white;
    box-shadow: 10px 10px #fff, 15px 15px #fff;
    box-shadow: getShow(100);
    animation: moveUp 10s linear infinite;
    // 利用为元素
    &::after{
        content: '';
        position: fixed;
        left: 0;
        top: 100vh;
        width: $size;
        height: $size;
        border-radius: inherit;
        box-shadow: inherit;
    }
}

基本上我们的第一层就大功告成了。

关于第二层和第三层我们不可能使用复制粘贴这种办法,当然使用到我们的for循环来完成。再次此前我们要想清楚进大远小的原理。三层的大小速度肯定是会有区别的。

 最后就达到了我们想要的效果。

 

 最后把源码附上。(html在文章开头)

*{
    padding: 0;
    margin: 0;
}
body{
    width: 100vw;
    height: 100vh;
    background-color: black;
}

@function getShow($n){
    $shadow: unquote('#{random(100)}vw') unquote('#{random(100)}vh') #fff;
    @for $i from 2 through $n {
        $shadow: '#{$shadow}' + ',' + '#{unquote('#{random(100)}vw') unquote('#{random(100)}vh') #fff}'
    }
    

    @return unquote($string: $shadow)
}
$count: 400;
$time: 1000;
@for $i from 1 through 3 {
    $size: #{$i}px;
    $count: floor($number: $count/2);
    $time: floor($number: $time/2);
    .layer#{$i}{
        position: fixed;
        width: $size;
        height: $size;
        top: 0;
        left: 0;
        border-radius: $size;
        background-color: white;
        box-shadow: 10px 10px #fff, 15px 15px #fff;
        box-shadow: getShow($count);
        animation: moveUp #{$time}s linear infinite;
        // 利用为元素
        &::after{
            content: '';
            position: fixed;
            left: 0;
            top: 100vh;
            width: $size;
            height: $size;
            border-radius: inherit;
            box-shadow: inherit;
        }
    }
}

@keyframes moveUp {
    100% {
        transform: translateY(-100vh);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【用于解决 failed Error: not found: python2 node-sass】 报错信息如下: ``` npm WARN prefer global [email protected] should be installed with -g > node-sass@4.5.2 install E:\workspace_vscode\ww\node_modules\node-sass > node scripts/install.js Downloading binary from https://github.com/sass/node-sass/releases/download/v4.5 .2/win32-x64-48_binding.node Cannot download "https://github.com/sass/node-sass/releases/download/v4.5.2/win3 2-x64-48_binding.node": connect ETIMEDOUT 54.231.72.83:443 Timed out whilst downloading the prebuilt binary Hint: If github.com is not accessible in your location try setting a proxy via HTTP_PROXY, e.g. export HTTP_PROXY=http://example.com:1234 or configure npm proxy via npm config set proxy http://example.com:8080 > node-sass@4.5.2 postinstall E:\workspace_vscode\ww\node_modules\node-sass > node scripts/build.js gyp verb check python checking for Python executable "python2" in the PATH gyp verb `which` failed Error: not found: python2 gyp verb `which` failed at getNotFoundError ``` 这个问题有两个解决方案 1. 按照提示需要 python2 环境,安装python2环境确实可以解决, 网上好多这种(管理员身份执行)。但是当你本来就有python环境时,环境变量不能自动替换,整起来就很麻烦。 ``` npm install --global --production windows-build-tools ``` 2. 第二种解决方案 ,看另一句报错,资源被墙。 ``` Downloading binary from https://github.com/sass/node-sass/releases/download/v4.5.2/win32-x64-48_binding.node Cannot download "https://github.com/sass/node-sass/releases/download/v4.5.2/win32-x64-48_binding.node": ``` 下载此资源即可。下载后需要设置变量路径,防止它再次去下载。 可以设置环境变量 直接右键我的电脑--》属性--》高级系统设置--》环境变量--》添加 或者执行 ``` set SASS_BINARY_PATH=D:\nodejs\tools\node-sass\win32-x64-46_binding.node ``` 再次执行 npm install 成功

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值