[英]How to change background-repeat and background-size by javascript
I currently have this and it's working for me: 我目前有这个,它正在为我工作:
<script type='text/javascript'>
$(function () {
var body = $('body');
var backgrounds = [
'url(http://localhost:12448/css/images/back1.jpg) no-repeat',
'url(http://localhost:12448/css/images/back2.jpg) no-repeat'];
var current = 0;
function nextBackground() {
body.css(
'background',
backgrounds[current = ++current % backgrounds.length]);
setTimeout(nextBackground, 5000);
}
setTimeout(nextBackground, 5000);
body.css('background', backgrounds[0]);
});
</script>
I need to implement also this part of css code in to my javascript, so i can get dynamically changing background and the same css settings as it changes: 我还需要在JavaScript中实现这部分CSS代码,以便我可以动态更改背景和更改时的相同CSS设置:
body
{
background-image: url('images/overlay.png'), url('images/bg.jpg');
background-repeat: repeat, no-repeat;
background-size: auto, 100% 100%;
}
Can anyone help me with this? 谁能帮我这个?
The main problem here is that you are setting the background
attribute, which overwrites all of the -size
, -repeat
, and -image
attributes. 这里的主要问题是,你设置的
background
属性,这将覆盖所有的-size
, -repeat
和-image
属性。 Instead, change the background-image
attribute only. 而是仅更改
background-image
属性。
This will require changes to your urls, as you have appended no-repeat
to the ends of them. 这将需要更改您的网址,因为您在网址末尾附加了
no-repeat
的内容。
function
s inside a jQuery ready block. function
。 var body = $('body'); var backgrounds = [ 'url(http://fineprintnyc.com/images/blog/history-of-logos/google/google-logo.png)', 'url(http://www.logospike.com/wp-content/uploads/2014/11/Google_logo-8.jpg)' ]; var current = 0; $(function() { setTimeout(nextBackground, 5000); body.css('background-image', backgrounds[0]); }); function nextBackground() { body.css( 'background-image', backgrounds[current = ++current % backgrounds.length]); setTimeout(nextBackground, 5000); }
html{ margin: 0; height: 100%; } body { background-image: url('images/overlay.png'), url('images/bg.jpg'); background-repeat: repeat, no-repeat; background-size: 100% 100%; height: 100%; margin: 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.