简体   繁体   English

刷新闪烁(Tumblr)的随机背景

[英]Random Background on Refresh Flicker (Tumblr)

I've been trying to get a working 'random background on refresh' on my Tumblr theme. 我一直在尝试在Tumblr主题上获得有效的“刷新时随机背景”。 For those that don't know, Tumblr themes are made up of HTML with embedded CSS and JavaScript. 对于那些不知道的人,Tumblr主题由带有嵌入式CSS和JavaScript的HTML组成。

Right now , I'm using this script: 现在,我正在使用以下脚本:

<script type="text/javascript">
        var bg1 = 'http://i.imgur.com/image1.jpg';
        var bg2 = 'http://i.imgur.com/image2.jpg';
        var bg3 = 'http://i.imgur.com/image3.jpg';
        var bg4 = 'http://i.imgur.com/image4.jpg';
        var bg5 = 'http://i.imgur.com/image5.jpg';

        var randBG=[bg1,bg2,bg3,bg4,bg5];

        window.onload=function() {
           num=Math.floor(Math.random()*randBG.length);
           document.body.style.background='url('+randBG[num]+') no-repeat center center fixed';
</script>

It works, but it causes screen flicker and nullifies my "background-size:cover" property. 它可以工作,但是会导致屏幕闪烁,并使我的“ background-size:cover”属性无效。 I've tried image preloading, with no success. 我尝试了图像预加载,但没有成功。 Tumblr doesn't support php, and I don't have a way of hosting a php file to link to. Tumblr不支持php,并且我没有托管链接到php文件的方法。

So, two things. 所以,两件事。 How can I randomize backgrounds on refresh without screen flicker, and, in the process, maintain the following CSS properties? 如何在刷新时随机化背景而不会出现屏幕闪烁,并在此过程中保持以下CSS属性?

-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;

First of all, the flickering is depending on the pre-fetch and the size of the background image. 首先,闪烁取决于预取和背景图像的大小。 So if the background image is HUGE it's not going to be ready at the same time as the document. 因此,如果背景图片很大,则无法与文档同时准备就绪。 Secondly, calling this on load event means you're waiting until everything is loaded on the screen before loading the background and therefore, you're upping the delay. 其次,调用此on load事件意味着您要等到所有内容都加载到屏幕上之后再加载背景,因此会增加延迟。 Thirdly, like I said in my comment, you're blowing away your background-size within your Javascript so you need to do it differently. 第三,就像我在评论中说的,您在Javascript中浪费了background-size ,因此您需要以不同的方式进行操作。

Here's what I did. 这就是我所做的。

CSS Snippet CSS片段

<style>
...
body {
  background: url('') no-repeat center center fixed;
  -moz-background-size: cover;
  background-size: cover;
}
...
</style>

Javascript Java脚本

<script type="text/javascript">
    $(document).ready(function(){
        var bg1 = 'http://i.imgur.com/enkkn.jpg';
        var bg2 = 'http://i.imgur.com/BZBke.jpg';
        var bg3 = 'http://i.imgur.com/QOvQH.jpg';
        var bgs = [bg1, bg2, bg3];
        var num = Math.floor(Math.random() * bgs.length);
        var bg = new Image();
        bg.src = bgs[num];
        $(bg).load(function(){
            document.body.style.backgroundImage = "url(" + this.src + ")";
        });
    });
</script>

Note: These background images are actually huge and I don't recommend using something that big. 注意:这些背景图片实际上很大,我不建议使用太大的图片。 Keep them compressed and small so they load faster. 使它们压缩并保持较小,以便它们加载更快。

I used jQuery since it has the ability to run something as soon as the $(document).ready event fires, which means it fires as soon as the document is ready in the background. 我使用jQuery是因为它有能力在$(document).ready事件触发时立即运行某些东西,这意味着一旦文档在后台准备就绪,它就会触发。 I also wait until the image is actually loaded for use and only then do I back it the background image. 我还要等到实际加载图像以供使用,然后才将其备份为背景图像。 I don't believe you'll be able to completely eliminate the flickering, but I think this will help. 我不认为您可以完全消除闪烁,但是我认为这会有所帮助。

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

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