简体   繁体   English

如何在我的网站上添加循环样式重复图像相位背景?

[英]How do I add a cycle style repeating image phase background to my website?

The question title says it all, I am not sure how to organize it in to my websites HTML due to the fixed menu bar, and its over all build. 问题标题说明了一切,由于固定的菜单栏及其全部构建,我不确定如何将其组织到我的网站HTML中。 So to say, I want my website to have multiple backgrounds that fade in and out. 可以这么说,我希望我的网站具有淡入淡出的多个背景。 I intend on adding more backgrounds over time. 我打算随着时间的推移增加更多背景。 What I listed below is what I've been attempting to work with. 我在下面列出的是我一直尝试使用的内容。

body {
      width: 400px;
      height: 400px;
     }
    /* set `#slideshow` parent background color */
    .slideshow {
      background: #000;
      display:block;
      width:inherit;
      height:inherit;
    }
    #slideshow {
      width: 100%;
      height: 100%;
      display: block;
      opacity: 0.0;
      background-color: #000;
      /* 
         set background images as `url(/path/to/image)` here, 
         separated by commas 
      */
      background-image: url("http://lorempixel.com/400/400/cats/?1"), 
        url("http://lorempixel.com/400/400/animals/?2"), 
        url("http://lorempixel.com/400/400/nature/?3"), 
        url("http://lorempixel.com/400/400/technics/?4"), 
        url("http://lorempixel.com/400/400/city/?5");
      background-size: cover, 0px, 0px, 0px;
    /* set transtitions at 3000ms 
      -webkit-transition: background-image 3000ms linear;
      -moz-transition: background-image 3000ms linear;
      -ms-transition: background-image 3000ms linear;
      -o-transition: background-image 3000ms linear;
      transition: background-image 3000ms linear;
        */
    }   

Javascript below. 下面的Javascript。

 $(function() {
  $.fx.interval = 0;
  (function cycleBgImage(elem, bgimg) {
    // `elem`:`#slideshow`
    // set, reset, delay to `1000` after background image reset
    elem.css("backgroundImage", bgimg)
      // fade in background image
      .fadeTo(3000, 1, "linear", function() {
        // fade in background image
        $(this).delay(3000, "fx").fadeTo(3000, 0, "linear", function() {
          // split background image string at comma , creating array
          var img = $(this).css("backgroundImage").split(","),
            // concat first background image to `img` array,
            // remove first background image from `img` array
            bgimg = img.concat(img[0]).splice(1).join(",");
          // recursively call `cycleBgImage`
          cycleBgImage(elem, bgimg);
        });
      });
  }($("#slideshow")));
});    

The division script, which I'm not sure I have a use for unless I make my whole website one large div which seems pointless. 除非我将整个网站设为一个大div,这似乎毫无意义,否则我不确定我是否会用到该划分脚本。

 <div class="slideshow">

Here is a quick hack. 这是一个快速的技巧。 I would probably do something more elegant with management of the images in an array, but this should get you going. 对于阵列中的图像管理,我可能会做些更优雅的事情,但这应该可以助您一臂之力。

 function swap(){ var $targets = $("#slideshow img"); var className = "active"; var $next = $targets.filter(".active").next(); if ($next.length === 0) { $next = $targets.first(); } $targets.removeClass(className); $next.addClass(className) } swap(); window.setInterval(swap, 5 * 1000); 
 #slideshow { background-color: #000; width: 400px; height: 400px; border: solid 1px; overflow: hidden; position: relative; } #slideshow img { position:absolute; opacity: 0; transition: opacity 2s ease-in-out; } #slideshow img.active { opacity: 1; } 
 <div id="slideshow"> <img src="http://lorempixel.com/400/400/cats/?1" /> <img src="http://lorempixel.com/400/400/animals/?2" /> <img src="http://lorempixel.com/400/400/nature/?3" /> <img src="http://lorempixel.com/400/400/technics/?4" /> <img src="http://lorempixel.com/400/400/city/?5" /> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

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

相关问题 如何在我的 html/css 网站上垂直添加“填充”点导航样式 - How do I add a 'fill up' dot navigation style vertically on my html/css website 如何使用element.style.backgroundImage =“ url(filePath)”将背景图像添加到javascript? - How do I add a background image to javascript using element.style.backgroundImage = “url(filePath)”? 如何使用 Chrome 扩展程序更改网站中的“样式”图像 - How Do I Change a "style" Image in a Website Using a Chrome Extension 如何加快网站背景图片的加载速度? - How can I speed up the loading of the background image for my website? 如何使用JavaScript更改背景图片在网站上的位置? - How do I change the background image position on a website using javascript? 如何将 particle.js 背景添加到此网站模板? - How do I add particle.js background to this website template? 如何在背景图片下添加背景颜色? - How do I add background color under background image? 如何在我的网站上添加“添加到收藏夹”按钮或链接? - How do I add an "Add to Favorites" button or link on my website? 如何在没有HTML的情况下在网站上打印图像 - How do I print an image in my website without the HTML 如何在我的React网站上添加分页 - How do I add pagination to my react website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM