【问题标题】:jQuery Body CSS Background URL if Empty Random URL如果为空随机 URL,则 jQuery Body CSS 背景 URL
【发布时间】:2016-03-03 17:42:18
【问题描述】:

我正在尝试查看页面上的 CSS 中的正文样式背景是否在 URL 中没有任何内容,然后淡入和淡出一组图像。

这是我的代码,但它似乎没有做任何事情或抛出任何错误

    <style>
html {

}
body
{
    background-color: transparent;
    background: url([%MEDIA_URL%]) no-repeat center center fixed;
    background-size: cover;
}
@media (max-width: 1024px) {
    html {
        background: url([%MEDIA_URL%]) no-repeat center center fixed;
        background-size: cover;
        height: 100%;
        overflow: hidden;
    }
    body
    {
        background-color: transparent;
        background: none;
        height:100%;
        overflow: scroll;
        -webkit-overflow-scrolling: touch;
    }
}
</style>
<script>
$(document).ready(function () {
    if ($('body').css("background") === 'url()') {

        (function($) { 

            var bgImageArray = ['exterior.jpg', 'chairs.jpg', 'entrance-logo.jpg'],
            base = "http://stage.houstonian.com/resources/1/BG_Images/",
            secs = 6;
            bgImageArray.forEach(function(img){
                new Image().src = base + img; 
                // caches images, avoiding white flash between background replacements
            });

            function backgroundSequence() {
            console.log('url(' + base + bgImageArray[0] +')');
                window.clearTimeout();
                var k = 0;
                for (i = 0; i < bgImageArray.length; i++) {
                    setTimeout(function(){ 
                        $('body').css({
                    'background': "url(" + base + bgImageArray[k] + ")"
                });
                    if ((k + 1) === bgImageArray.length) { setTimeout(function() { backgroundSequence() }, (secs * 1000))} else { k++; }            
                    }, (secs * 1000) * i)   
                }
            }
            backgroundSequence();  

        }(jQuery));

    }
});
</script>

任何帮助将不胜感激。我从这个工作小提琴中获得的背景图像数组。 https://jsfiddle.net/sm1215/r5or5w6m/3/

让它随机也很好:)

【问题讨论】:

  • 此外,[%MEDIA_URL%] 是 CMS 使用的标签,如果没有为页面上传图片,则不会显示。
  • 你知道你可以很容易地test things like this自己,并且看到它可能永远不会返回字符串url() ?
  • 测试是否$('body').css("background-image") == 'none'
  • 为什么不返回url字符串?是否有可能做到这一点?小提琴适用于 css 背景。
  • 但是在小提琴中,你看到控制台中的字符串'url()'了吗?即使只是设置那个 CSS 属性也不会返回那个字符串......永远 -> jsfiddle.net/u7qp883m/1

标签: jquery css background


【解决方案1】:

这个怎么样?

更新!现在完成一个STOP按钮!

if (!window.hasOwnProperty('bgChanger')) {
	window.bgChanger = {
		baseHref: 'http://stage.houstonian.com/resources/1/BG_Images/',
		imgArray: ['exterior.jpg', 'chairs.jpg', 'entrance-logo.jpg'],
		secs: 2,  //  changed seconds to 2 just for quick show
		tmr: void 0,
		init: function() {
			bgChanger.setBGImg();
			bgChanger.sequencer();
        },
		getNextImgPath: function() {	//	will get random image path
			return this.baseHref + this.imgArray[~~(Math.random() * (this.imgArray.length - 0))];
		},
		sequencer: function() {
			if (this.tmr) clearTimeout(this.tmr);
			this.tmr = setTimeout(this.init, this.secs*1e3);
		},
		setBGImg: function() {
			var imgPath = this.getNextImgPath(),
				current = $('body').css('backgroundImage');
			//	this while statement helps ensure a different image everytime
			while (current.match(imgPath)) imgPath = this.getNextImgPath();
			$('body').css('backgroundImage', 'url('+imgPath+')');
		},
		stop: function() { if (this.tmr) clearTimeout(this.tmr); }
	}
	//	preloader
	bgChanger.imgArray.forEach(function(img) { new Image().src=bgChanger.baseHref+img;  });
}


$(function() {
	if (!$('body').css("background-image").match(/url\(.*(gif|jpg|jpeg|tiff|png)/i)) bgChanger.init();
	
	$('button').click(function(e) {
		if ($(this).text() == 'STOP') {
			$(this).text('GO');
			bgChanger.stop();
		}
		else {
			$(this).text('STOP');
			bgChanger.init();
		}
	});
	
})
html, body { height: 100%; width: 100%; }
body {
  background-color: transparent;
  background: url() no-repeat center center fixed;
  background-size: cover;
}

table { height: 100%; width: 100%; }
th { height: 100%; text-align: center; vertical-align: middle; }
button { font-size: 2em; padding: 1em 1.5em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table><tr><th><button>STOP</button></th></tr></table>

【讨论】:

  • @Aaron 仅供参考,由于您的评论,我对其进行了小改动,可能需要更新
  • 是否有这个 /url(*.jpg/ 意味着如果 url 为空它会运行,如果它在 url () 中有东西它不会?
  • @Aaron 这意味着background-image 必须有url([anything-here].jpg) 如果不是所有的背景图片都是jpgs,你会想要稍微改变它
  • 系统将css渲染成这样 background: url([%MEDIA_URL%]) no-repeat center center fixed;如果他们没有放置图像,则会呈现背景: url() no-repeat center center fixed;所以我们希望你的 AWESOME 代码能够运行。
  • @Aaron 我改变了我的答案,以更涵盖不同的图像类型并考虑大写或小写
猜你喜欢
  • 2021-09-22
  • 2014-02-25
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
相关资源
最近更新 更多