【问题标题】:Random background image for each div with the same class具有相同类的每个 div 的随机背景图像
【发布时间】:2018-07-14 17:30:48
【问题描述】:

我想使用.item 类为每个 div 应用随机背景。

我尝试用 PHP 和 CSS 来做这件事,像这样:

<?php
$bg = array('bg1.jpg', 'bg2.jpg', 'bg3.jpg', 'bg4.jpg', 'bg5.jpg' );
$i = rand(0, count($bg)-1);
$selectedBg = "$bg[$i]";
?>

.item {
  background: url(example.com/images/<?php echo $selectedBg; ?>);
}

它有效,但每个 div 都有完全相同的背景图像(例如 bg3.jpg),我希望每个单独的 div 有不同的随机背景(我不介意它们是否偶尔重复一次)。

我试过了:

.item {
background: url(<?php echo "example.com/images/bg".rand(1, 5).".jpg"; ?>)

结果相同。

我想我需要使用 JavaScript(我完全没有经验)来实现这一点。我找到了随机背景颜色的工作解决方案: http://jsfiddle.net/VXG36/1/

我也想做同样的事情,但使用背景图片。

【问题讨论】:

  • 如果所有div 元素都使用同一个类,并且您设置了此类的backgrond 属性,则将为所有div 元素设置背景。也许您想使用不同的类或给您的元素一个单独的 ID?
  • @BenjaminJ。您不必在类上设置background-image。 OP 说他想让所有div.item 元素都有一个随机背景,所以你只需要影响这些元素。您不需要在类样式规则中设置图像。
  • @ScottMarcus:是的,我知道。这就是我的评论的意思。
  • @BenjaminJ。这根本不是听起来的样子。听起来你不明白要求什么。不需要单独的课程和/或ids。

标签: javascript php jquery css random


【解决方案1】:

只需将任何图像 URL 添加到数组中。添加的图片越多,重复的机会就越少。

请参阅下面的内联 cmets 以获得解释:

// Set up your array with the URLs of the background images you'll want:
var randomImagess = [
 "http://laoblogger.com/images/image-of-smiley-face-6.jpg",
 "http://images.clipartpanda.com/smiley-face-clip-art-RcGG8gecL.jpeg",
 "http://images.clipartpanda.com/smiley-face-thumbs-up-thank-you-10_smiley_face.jpg",
 "https://clipartion.com/wp-content/uploads/2015/12/green-smiley-face-images-stock-free-green-830x830.jpg",
 "https://image.freepik.com/free-photo/cartoon-smile-samuel-smiles-smiley-face-team_121-67198.jpg",
 "https://i.pinimg.com/236x/ed/e7/08/ede7084f513353a377a4e841d7ba90ed--smiley-face-images-smiley-faces.jpg",
 "https://cdn.schoolstickers.com/products/en/819/GREEN-SMILE-00.png",
 "https://buildahead.com/wp-content/uploads/2017/02/happy-emoji-smaller-300x300.png",
 "https://i.pinimg.com/236x/85/7d/22/857d22615fd70ccbfa10afdffff0eb1b--mad-face-smiley-faces.jpg"
];
    
// Get all the div elements with the item class into a collection
var divs = document.querySelectorAll("div.item");

// Convert the collection into an array so that we can loop with .forEach
var divArray = Array.prototype.slice.call(divs);

//Loop over the array...
divArray.forEach(function(div) {
  // Get a random number from 0 to the highest index in the array
  var randomNum = Math.floor(Math.random() * randomImagess.length);
  // Set the backgroundImage property to the random URL
  div.style.backgroundImage = "url(" + randomImagess[randomNum] + ")";
});
div.item {
  /* These styles are just for making sure the demo fits in the preview area. */
  width:175px;
  height:175px;
  float:left;
  border:1px solid black;
  
  /* Use background-size:contain to fit the entire image into the div's space. This 
     wil make the image zoom in/out as needed to completely fit.
     Or, use background-size:cover to scale the image to fit the space of the div. */
  background-size:cover; 
}
<div class="item">Div 1</div>
<div class="item">Div 2</div>
<div class="item">Div 3</div>

【讨论】:

  • 谢谢!这正是我的意思。但是,它对我不起作用...我将代码放在 的头部。我做错了什么?
  • @Barcin 确保您的script 位于body 标签结束之前的文件中。如果你把它放在head 中,它会在div 元素被解析到内存之前搜索它们。通过将脚本放在body 结束之前,所有其他 HTML 元素都已添加到内存中,并且可以被 JavaScript 找到。
【解决方案2】:

只需将你的 php 代码封装到一个函数中,然后为每个 div 使用内联样式,因为关于你重复类定义的地方更少,最后一个定义只会起作用。

例如:

    <?php
    function randImage(){
       $bg = array('bg1.jpg', 'bg2.jpg', 'bg3.jpg', 'bg4.jpg', 'bg5.jpg' );
       $i = rand(0, count($bg)-1);
       $selectedBg = "$bg[$i]";
       return $selectedBg;
    }

    ?>
    <div style="background: url(example.com/images/bg<?php echo randImage(); ?>)">Div 1</div>
    <div style="background: url(example.com/images/bg<?php echo randImage(); ?>)">Div 2</div>
<div style="background: url(example.com/images/bg<?php echo randImage(); ?>)">Div 3</div>

【讨论】:

  • 谢谢,但是这些 div 是由插件自动生成的,我无法单独设置它们的样式(或者,如果可以,我不想这样做,因为它们会很多) .还是谢谢!
  • @Barcin 所以适合您的解决方案是使用客户端解决方案,即 JavaScript,例如 Scott 的解决方案。
猜你喜欢
  • 2015-10-26
  • 2010-11-27
  • 2013-01-14
  • 1970-01-01
  • 2013-04-08
  • 2014-10-12
  • 2016-01-17
  • 2015-04-02
  • 1970-01-01
相关资源
最近更新 更多