简体   繁体   English

如何通过javascript更改背景重复和背景大小

[英]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? 谁能帮我这个?

Solution: 解:

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的内容。


Some syntactical good practices: 一些语法上的良好做法:

  1. Don't define function s inside a jQuery ready block. 不要在jQuery ready块中定义function
  2. Put your pertinent variables outside the jQuery block so that they are global and can be used in the function. 将您的相关变量放在jQuery块之外,以便它们是全局的并且可以在函数中使用。

 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.

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