简体   繁体   English

HTML响应式CSS不适用于随机图像

[英]HTML Responsive css doesn't work with random image

The original template just used fixed image and made it responsive for different screen resolutions: 原始模板仅使用固定图像,并使其响应不同的屏幕分辨率:

   <div class="bg-img" style="background-image: url('./img/background1.jpg');">
   <div class="overlay"></div>
   </div>

Now, I add a random image and tried to make the class/style of the responsive, work here, but it doesn't. 现在,我添加了一个随机图像,并尝试使响应式的类/样式在这里起作用,但事实并非如此。

   <header id="home">

   <div class="bg-img" style="background-image">
   <div id="banner-load"></div>

<script type='text/javascript'
   src='https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js'> 
   </script>

   <script>
   var images = ['background11.jpg', 'background12.jpg', 'background13.jpg', 
   'background14.jpg', 
   'background15.jpg'];

    $('<img class="fade-in, bg-img" style="background-image" src="img/' + 
    images[Math.floor(Math.random() * images.length)] + '">').appendTo('#banner- 
    load');
    </script>

    <div class="overlay"></div>
    </div>

What am I doing wrong? 我究竟做错了什么? may be the class="bg-img" style="background-image" doesn't recognize the random image? 可能是class =“ bg-img” style =“ background-image”无法识别随机图像? if so, how can it be solved? 如果是这样,如何解决?

Update 更新

I am trying something else, taking the original code using the div for the background, and making it programmable: 我正在尝试其他方法,将原始代码使用div作为背景,并使其可编程:

$("<div class='bg-img' style="background-image: url('img/" + images[Math.floor(Math.random() * images.length)] + ");'>")

but still I can't make it work. 但我仍然无法正常工作。 Something is wrong and I get an error 出问题了,我得到一个错误

--> Uncaught SyntaxError: missing ) after argument list ->参数列表后未捕获到的SyntaxError:缺少)

 <header id="home"> <div class="bg-img" style="height:100%;width:100%;background-image:none;background-size:cover;"> <div id="banner-load"></div> <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js'> </script> <script> var images = ['background11.jpg', 'background12.jpg', 'background13.jpg', 'background14.jpg', 'background15.jpg']; function getRandomImage() { return 'img/'+images[Math.floor(Math.random() * images.length)]; } function setRandomImage() { $('.bg-img').css('backgroundImage', 'url('+getRandomImage()+')'); } $(function(){ setRandomImage(); }); </script> <div class="overlay"></div> </div> 

Normally I would use img instead of div for displaying an image. 通常我会使用img而不是div来显示图像。 I had to set a width and height for the div since it had nothing in it. 我必须为div设置宽度和高度,因为其中没有任何内容。 This can be something like 100px or to be responsive - I used 100% (to fill the current view). 这可以是100px之类的,也可以是响应性的-我用了100%(用于填充当前视图)。

I used background-size:cover so that the image fills the div no matter what size it is. 我使用background-size:cover来使图像填充div,无论它是什么尺寸。

Use this : 用这个 :

$('.bg-image').css('backgroundImage', 'img/' + images[Math.floor(Math.random() * images.length)]);

Instead of 代替

$('<img class="fade-in, bg-img" style="background-image" src="img/' + 
images[Math.floor(Math.random() * images.length)] + '">').appendTo('#banner- 
load');

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

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