如何在不刷新页面的情况下“连续”更改背景颜色

时间:2016-12-12 11:22:18

标签: javascript jquery css jquery-ui

这里有什么方法可以像彩虹那样连续改变背景颜色吗?

3 个答案:

答案 0 :(得分:7)

希望你需要这样的东西。

var body = $('body');
var colors = ['red', 'green', 'blue', 'yellow', 'pink', 'purple'];
var currentIndex = 0;
setInterval(function () {
   body.css({
     backgroundColor: colors[currentIndex]
   });
   if (!colors[currentIndex]) {
       currentIndex = 0;
   } else {
       currentIndex++;
   }
}, 100);
body {
  transition: 200ms ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:6)

对于颜色,您可能需要使用CSS animation

<div class="wrapper"></div>

.wrapper { 
  height: 100%;
  width: 100%;
  left:0;
  right: 0;
  top: 0;
  bottom: 0;
  position: absolute;
background: linear-gradient(124deg, #ff2400, #e81d1d, #e8b71d, #e3e81d, #1de840, #1ddde8, #2b1de8, #dd00f3, #dd00f3);
background-size: 1800% 1800%;

-webkit-animation: rainbow 18s ease infinite;
-z-animation: rainbow 18s ease infinite;
-o-animation: rainbow 18s ease infinite;
  animation: rainbow 18s ease infinite;}

@-webkit-keyframes rainbow {
    0%{background-position:0% 82%}
    50%{background-position:100% 19%}
    100%{background-position:0% 82%}
}
@-moz-keyframes rainbow {
    0%{background-position:0% 82%}
    50%{background-position:100% 19%}
    100%{background-position:0% 82%}
}
@-o-keyframes rainbow {
    0%{background-position:0% 82%}
    50%{background-position:100% 19%}
    100%{background-position:0% 82%}
}
@keyframes rainbow { 
    0%{background-position:0% 82%}
    50%{background-position:100% 19%}
    100%{background-position:0% 82%}
}

要在图像之间切换,代码会有所不同:

<div class="topstrip" style="height:100px;width:100px;"></div>

    <script>
    var imagebackground = 0;
    var imgbackgrounds = [];
    imgbackgrounds[0] = '/images/image1.jpg';
    imgbackgrounds[1] = '/images/image2.jpg';
    imgbackgrounds[2] = '/images/image3.jpg';
    imgbackgrounds[3] = '/images/image4.jpg';
    imgbackgrounds[4] = '/images/image5.jpg';


    function changeimage() {
        imagebackground++;
        if(imagebackground > 4) imagebackground = 0;

        $('.topstrip').fadeToggle("slow",function() {
            $('.topstrip').css({
                'background-image' : "url('" + backgrounds[imagebackground] + "')"
            });
            $('.topstrip').fadeToggle("slow");
        });


        setTimeout(changeimage, 5000);
    }

    $(document).ready(function() {
        setTimeout(changeimage, 5000);        
    });
    </script>

答案 2 :(得分:6)

我希望你的意思是彩虹效应?这个CSS代码可以提供它:

.wrapper { 
  height: 100%;
  width: 100%;
  left:0;
  right: 0;
  top: 0;
  bottom: 0;
  position: absolute;
background: linear-gradient(124deg, #ff2400, #e81d1d, #e8b71d, #e3e81d, #1de840, #1ddde8, #2b1de8, #dd00f3, #dd00f3);
background-size: 1800% 1800%;

-webkit-animation: rainbow 18s ease infinite;
-z-animation: rainbow 18s ease infinite;
-o-animation: rainbow 18s ease infinite;
  animation: rainbow 18s ease infinite;}

@-webkit-keyframes rainbow {
    0%{background-position:0% 82%}
    50%{background-position:100% 19%}
    100%{background-position:0% 82%}
}
@-moz-keyframes rainbow {
    0%{background-position:0% 82%}
    50%{background-position:100% 19%}
    100%{background-position:0% 82%}
}
@-o-keyframes rainbow {
    0%{background-position:0% 82%}
    50%{background-position:100% 19%}
    100%{background-position:0% 82%}
}
@keyframes rainbow { 
    0%{background-position:0% 82%}
    50%{background-position:100% 19%}
    100%{background-position:0% 82%}
}

来源:https://codepen.io/nohoid/pen/kIfto