利用 HTML 数据属性设置 CSS 背景图像网址

我计划建立一个自定义图片库的朋友,我确切地知道我将如何生产的 HTML,但我遇到了一个小问题与 CSS。< br > (如果可能的话,我不希望页面样式依赖于 jQuery)


我的问题是:
HTML 中的 Data-Attribute
CSS 中的 Background-image
我在 HTML 缩略图中使用这种格式:
<div class="thumb" data-image-src="images/img.jpg"></div>

我假设 CSS 应该是这样的:

.thumb {
width:150px;
height:150px;
background-position:center center;
overflow:hidden;
border:1px solid black;


background-image: attr(data-image-src);/*This is the question piece*/
}


我的目标是从我的 HTML 文件中的 div.thumb获取 data-image-src,并将其用于 CSS 文件中的每个 div.thumb(s) background-image源。

下面是一支 Codepen Pen 笔,以便获得一个动态示例,说明我在寻找什么:
Http://codepen.io/thestevekelzer/pen/redjv

141852 次浏览

You will need a little JavaScript for that:

var list = document.getElementsByClassName('thumb');


for (var i = 0; i < list.length; i++) {
var src = list[i].getAttribute('data-image-src');
list[i].style.backgroundImage="url('" + src + "')";
}

Wrap that in <script> tags at the bottom just before the </body> tag or wrap in a function that you call once the page loaded.

You will eventually be able to use

background-image: attr(data-image-src url);

but that is not implemented anywhere yet to my knowledge. In the above, url is an optional "type-or-unit" parameter to attr(). See https://drafts.csswg.org/css-values/#attr-notation.

HTML

<div class="thumb" data-image-src="img/image.png">

jQuery

$( ".thumb" ).each(function() {
var attr = $(this).attr('data-image-src');


if (typeof attr !== typeof undefined && attr !== false) {
$(this).css('background', 'url('+attr+')');
}


});

Demo on JSFiddle

You could do this also with JavaScript.

It is not best practise to mix up content with style, but a solution could be

<div class="thumb" style="background-image: url('images/img.jpg')"></div>

How about using some Sass? Here's what I did to achieve something like this (although note that you have to create a Sass list for each of the data-attributes).

/*
Iterate over list and use "data-social" to put in the appropriate background-image.
*/
$social: "fb", "twitter", "youtube";


@each $i in $social {
[data-social="#{$i}"] {
background: url('#{$image-path}/icons/#{$i}.svg') no-repeat 0 0;
background-size: cover; // Only seems to work if placed below background property
}
}

Essentially, you list all of your data attribute values. Then use Sass @each to iterate through and select all the data-attributes in the HTML. Then, bring in the iterator variable and have it match up to a filename.

Anyway, as I said, you have to list all of the values, then make sure that your filenames incorporate the values in your list.

HTML CODE

<div id="borderLoader"  data-height="230px" data-color="lightgrey" data-
width="230px" data-image="https://fiverr- res.cloudinary.com/t_profile_thumb,q_auto,f_auto/attachments/profile/photo/a54f24b2ab6f377ea269863cbf556c12-619447411516923848661/913d6cc9-3d3c-4884-ac6e-4c2d58ee4d6a.jpg">


</div>

JS CODE

var dataValue, dataSet,key;
dataValue = document.getElementById('borderLoader');
//data set contains all the dataset that you are to style the shape;
dataSet ={
"height":dataValue.dataset.height,
"width":dataValue.dataset.width,
"color":dataValue.dataset.color,
"imageBg":dataValue.dataset.image
};


dataValue.style.height = dataSet.height;
dataValue.style.width = dataSet.width;
dataValue.style.background = "#f3f3f3 url("+dataSet.imageBg+") no-repeat
center";

For those who want a dumb down answer like me

Something like how to steps as 1, 2, 3

Here it is what I did

First create the HTML markup

<div class="thumb" data-image-src="images/img.jpg"></div>

Then before your ending body tag, add this script

I included the ending body on the code below as an example

So becareful when you copy

<script>
var list = document.getElementsByClassName('thumb');


for (var i = 0; i < list.length; i++) {
var src = list[i].getAttribute('data-image-src');
list[i].style.backgroundImage="url('" + src + "')";
}
</script>


</body>

If you wanted to keep it with just HTML and CSS you can use CSS Variables. Keep in mind, css variables aren't supported in IE.

<div class="thumb" style="--background: url('images/img.jpg')"></div>
.thumb {
background-image: var(--background);
}

Codepen: https://codepen.io/bruce13/pen/bJdoZW

Here is simple example using jQuery we can put the images in background

$('*[data-background-image]').each(function() {
$(this).css({
'background-image': 'url(' + $(this).data('background-image') + ')'
});
});
div{
height:200px;
width:100% ;
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div data-background-image="https://via.placeholder.com/500"> </div>