简体   繁体   English

为视差背景添加滚动图像

[英]adding scrolling images for a parallax background

I am trying to make multiple images (3) fade in and out as parallax background. 我正在尝试使多个图像(3)作为视差背景淡入和淡出。 I am currently using a large animated gif which is not going to cut it due to the loading times and what I eventually need. 我当前使用的是大型动画gif,由于加载时间以及最终所需的内容,因此无法将其剪切。 I am trying to target a "data-background" attribute which I have done but can't seem to get the images to change. 我正在尝试定位一个已经完成但似乎无法更改图像的“数据背景”属性。 I can get it to output in the console but not the data-background. 我可以将其输出到控制台,但不能输出数据背景。 Below is the code. 下面是代码。

Thanks! 谢谢!

<section id="paralax-image" style="height: 400px;" class="module-cover parallax" data-background="" data-overlay="1"
        data-gradient="1">


(function () {

// The images array.
var images = ["assets2/Arcadian.jpg", "assets2/AngryPrawns.jpg", "assets2/Apricot_Smash.jpg"];

// The counter function using a closure.
var add = (function() {
    // Setting the counter to the last image so it will start with the first image in the array.
    var counter = images.length - 1;
    return function() {
        // When the last image is shown reset the counter else increment the counter.
        if(counter === images.length - 1) {
            counter = 0;
        } else {
            counter+=1;
        }
        return counter;
    }
})();

// The function for changing the images.
setInterval(
    function() {
      var section = document.getElementById("paralax-image");
      section.getAttribute("data-background");
        section.setAttribute('data-background', images[add()]);
        console.log(images[add()]);
    }
, 3000);

})();   

First of all, attributes that have "data-" in front of them are only used to store some custom data on elements. 首先,前面带有“ data-”的属性仅用于在元素上存储一些自定义数据。 Those attributes do not influence the appearance/behaviour of your app in any way unless you use them in your JS/CSS. 这些属性不会以任何方式影响应用程序的外观/行为,除非您在JS / CSS中使用它们。

So, in your code, you are setting the data-background attribute on your section. 因此,在您的代码中,您将在您的部分上设置data-background属性。 The code is working correctly and if you look into the inspector, you can actually see that that attribute's value is changing as expected. 代码运行正常,如果您查看检查器,您实际上可以看到该属性的值正在按预期方式变化。

The next step for you would be to display the images that you set in your data-background attribute - either using JS or CSS. 下一步是使用JS或CSS显示在data-background属性中设置的图像。

Unfortunately, for now, it's not possible to grab the background URL from attribute value in CSS as described in the top-voted answer here: Using HTML data-attribute to set CSS background-image url 不幸的是,目前尚无法从CSS的属性值中获取背景URL,如此处最受好评的答案所述: 使用HTML数据属性设置CSS背景图像url

However, you can still manually set the CSS background-image property using JavaScript based on the "data-" property. 但是,您仍然可以基于“ data-”属性,使用JavaScript手动设置CSS background-image属性。

 // The images array. const images = ["https://images.pexels.com/photos/255379/pexels-photo-255379.jpeg?auto=compress&cs=tinysrgb&h=350", "https://images.pexels.com/photos/531880/pexels-photo-531880.jpeg?auto=compress&cs=tinysrgb&h=350", "https://images.unsplash.com/photo-1530482817083-29ae4b92ff15?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=44f4aebbd1e1371d5bf7dc22016c5d29&w=1000&q=80"]; const imagesSwitcher = () => { const getCounter = () => { // Setting the counter to the last image so it will start with the first image in the array. let counter = images.length - 1; return () => { // When the last image is shown reset the counter else increment the counter. if(counter === images.length - 1) { counter = 0; } else { counter += 1; } return counter; } } const counter = getCounter(); const updateBackground = () => { const section = document.getElementById("paralax-image"); section.style.background = `url(${images[counter()]}) no-repeat`; }; updateBackground(); setInterval(() => updateBackground(), 3000); }; imagesSwitcher(); 
 .dynamic-background { display: block; width: 500px; height: 200px; background-size: 100%; } 
 <div> <section id="paralax-image" class="dynamic-background" style="height: 400px;" class="module-cover parallax" data-background="" data-overlay="1" data-gradient="1"> </section> </div> 

The thing is - in this case, you don't even actually need this data-background property. 关键是-在这种情况下,您甚至实际上不需要此data-background属性。 You can simply switch background image using JS. 您只需使用JS即可切换背景图片。

Now, it's not very clear what you meant by parallax in your case. 现在,还不清楚您所说的视差是什么意思。 In case you actually meant parallax background like in here http://jsfiddle.net/Birdlaw/ny8rqzu5/ , you would need to take a different approach overall. 如果您实际上是在这里http://jsfiddle.net/Birdlaw/ny8rqzu5/表示视差背景,则需要整体采用其他方法。 Please comment if you need any help with this. 如果您需要任何帮助,请发表评论。

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

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