简体   繁体   English

CSS中基于html标题值的图片url

[英]Image url in css based on html title value

I'm trying to generate multiple hexagons with background images, using a php for loop: 我正在尝试使用php for loop生成带有背景图像的多个六边形:

<?php
        $dir    = 'resources/images/logos';
        $images = scandir($dir);
        for($i = 2; $i < count($images); $i++) {
            $title = explode('.', $images[$i])[0];
            ?>
                <div class="hexagon_loc<?= $i ?>">
                    <div class="hexagon image" title="<?= $title ?>"></div>
                </div>
    <?php } ?>

This works fine, I make a div, with a different class name every time and with the correct title (based of on the filename of the image). 这很好,我创建了一个div,每次使用不同的类名并具有正确的标题(基于图像的文件名)。 Now I want to have a for loop in scss which draws the hexagons, I also want to define the background-images there. 现在我想在scss中有一个for循环来绘制六边形,我也想在那里定义背景图像。 But since I can't pass any kind of variables from html/php to css/sass and the fact that the attr() function is widely unsupported I have no way of dynamically doing this, which would result in the titles possibly being mismatched with the images. 但是由于我无法将任何类型的变量从html / php传递给css / sass,而且attr()函数不受广泛支持,因此我无法动态地执行此操作,这可能导致标题与图片。 Is there anyway to do this anyways? 反正有这样做吗? Or do I have to use a different approach entirely? 还是我必须完全使用其他方法?

Example sass where I want to define the image: 我想在其中定义图像的示例sass:

.hexagon_loc {
$image: '../../resources/images/bill.jpeg';
@include hexagon($image);
margin-left: 100px;
}

Using vanilla JavaScript to solve this would also be a possibility 使用香草JavaScript解决此问题也有可能

Thanks 谢谢

In this situation, I would use <img> HTML element. 在这种情况下,我将使用<img> HTML元素。

There is a CSS solution but it is not supported in many browsers. 有CSS解决方案,但许多浏览器均不支持

div { background-image : attr(data-image); }

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

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