简体   繁体   English

如何通过刷新更改CSS中的随机背景

[英]How to change a random background in CSS with refresh

I'm developing a website but I want to set a random background in CSS. 我正在开发一个网站,但我想在CSS中设置随机背景。

Right now, I have this code CSS: 现在,我有以下代码CSS:

#bg {
    -moz-transform: scale(1.0);
    -webkit-transform: scale(1.0);
    -ms-transform: scale(1.0);
    transform: scale(1.0);
    -webkit-backface-visibility: hidden;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh;
    z-index: 1;
}

    #bg:before, #bg:after {
        content: '';
        display: block;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
    }

    #bg:before {
        -moz-transition: background-color 2.5s ease-in-out;
        -webkit-transition: background-color 2.5s ease-in-out;
        -ms-transition: background-color 2.5s ease-in-out;
        transition: background-color 2.5s ease-in-out;
        -moz-transition-delay: 0.75s;
        -webkit-transition-delay: 0.75s;
        -ms-transition-delay: 0.75s;
        transition-delay: 0.75s;
        background-image: linear-gradient(to top, rgba(19, 21, 25, 0.5), rgba(19, 21, 25, 0.5)), url("../../images/overlay.png");
        background-size: auto, 256px 256px;
        background-position: center, center;
        background-repeat: no-repeat, repeat;
        z-index: 2;
    }

    #bg:after {
        -moz-transform: scale(1.125);
        -webkit-transform: scale(1.125);
        -ms-transform: scale(1.125);
        transform: scale(1.125);
        -moz-transition: -moz-transform 0.325s ease-in-out, -moz-filter 0.325s ease-in-out;
        -webkit-transition: -webkit-transform 0.325s ease-in-out, -webkit-filter 0.325s ease-in-out;
        -ms-transition: -ms-transform 0.325s ease-in-out, -ms-filter 0.325s ease-in-out;
        transition: transform 0.325s ease-in-out, filter 0.325s ease-in-out;
        background-image: url("../../images/bg_01.jpg");
        background-position: center;
        background-size: cover;
        background-repeat: no-repeat;
        z-index: 1;
    }

    body.is-article-visible #bg:after {
        -moz-transform: scale(1.0825);
        -webkit-transform: scale(1.0825);
        -ms-transform: scale(1.0825);
        transform: scale(1.0825);
        -moz-filter: blur(0.2rem);
        -webkit-filter: blur(0.2rem);
        -ms-filter: blur(0.2rem);
        filter: blur(0.2rem);
    }

    body.is-loading #bg:before {
        background-color: #000000;
    }

And I made this JS code to change the background: 我制作了以下JS代码来更改背景:

var images = ['bg_01.jpg',
          'bg_02.jpg', 
          'bg_03.jpg', 
          'bg_04.jpg', 
          'bg_05.jpg', 
          'bg_06.jpg', 
          'bg_07.jpg', 
          'bg_08.jpg', 
          'bg_09.jpg', 
          'bg_10.jpg'];
$('body').css({'background-image' : 'url(../../images/' + images[Math.floor(Math.random() * images.length)] + ')']);

I want to use the JS code in #bg:after, how can I do that? 我想在#bg中使用JS代码:之后,该怎么办? do I need to change something in the js code? 我需要在js代码中进行更改吗? the backgrounf works good because I'm using a free template but I want to keep that code because has animations, etc. and I just would like to make it random every refresh. backgrounf很好用,因为我使用的是免费模板,但我想保留该代码,因为它有动画等,我只想在每次刷新时使其随机。

PS: Sorry, but I'm noob in this. PS:对不起,但我对此不感兴趣。


CODE UPDATED AND WORKING 代码更新和工作

I've found the solution. 我找到了解决方案。 First I had to remove the line: 首先,我必须删除该行:

background-image: url("../../images/bg_01.jpg");

From #bg:after block. 来自#bg:after块。 Then, I changed my javascript code like this: 然后,我像这样更改了JavaScript代码:

var images = ['bg_01.jpg',
          'bg_02.jpg', 
          'bg_03.jpg', 
          'bg_04.jpg', 
          'bg_05.jpg', 
          'bg_06.jpg', 
          'bg_07.jpg', 
          'bg_08.jpg', 
          'bg_09.jpg', 
          'bg_10.jpg'];
$('#bg').css({'background-image' : 'url(images/' + images[Math.floor(Math.random() * images.length)] + ')'});

Now, my website change the background every refresh :) 现在,我的网站每次刷新都会更改背景:)

Thanks a lot for your help! 非常感谢你的帮助!

Your question is not very clear. 您的问题不是很清楚。 I assume that you want to have a div with a random background and transition between image and color. 我假设您想要一个具有随机背景的div,并在图像和颜色之间进行过渡。

First you js line should be... 首先你的js行应该是...

$('body').style.backgroundImage = "url(../../images/" + images[Math.floor(Math.random() * images.length)] + ")";

Here is an example of what I think you want to achieve. 这是我想实现的目标的一个示例。

 var div = document.getElementById("myDiv"); var images = [ "https://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Kitten_in_Rizal_Park%2C_Manila.jpg/1200px-Kitten_in_Rizal_Park%2C_Manila.jpg", "https://www.sdhumane.org/wp-content/uploads/2017/06/10k_nova-0.jpg", "https://4fi8v2446i0sw2rpq2a3fg51-wpengine.netdna-ssl.com/wp-content/uploads/2016/06/KittenProgression-Darling-week3.jpg", "http://www.catster.com/wp-content/uploads/2017/12/A-gray-kitten-meowing.jpg" ]; div.onmouseover = function() { div.style.backgroundImage = "url(" + images[Math.round(Math.random() * images.length)] + ")"; } 
 #myDiv { width: 250px; height: 250px; background-size: 250px 250px; } #colorOverlay { width: 250px; height: 250px; background: gold; -webkit-transition: opacity 0.5s; transition: opacity 0.5s; } #colorOverlay:hover { opacity: 0; } 
 <div id="myDiv"> <div id="colorOverlay"> </div> </div> 

(*This example has issues if you change the images too fast because completely loads them again every time.) (*如果您更改图像的速度过快,则此示例会出现问题,因为每次都会完全重新加载它们。)

It sets a new image when the mouse is hovered over the div. 当鼠标悬停在div上时,它将设置一个新图像。 You can instead do it once when the page loads. 您可以改为在页面加载后执行一次。 To fade from color to the image, the only way I think there is is to have two divs, one with the image, one with the color. 要从颜色逐渐淡入图像,我认为唯一的方法是有两个div,一个包含图像,一个包含颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM