如何在单击时将 IMG SRC 属性添加到 SPAN CSS 背景图像中
How to add IMG SRC attribute into SPAN CSS background-image on click
如何在每次单击#thumbs1 时将#largeImage1 SRC 属性添加到跨度背景图像中?它应该使用新的 SRC 更新跨度背景图像。
我似乎无法让它工作。我的语法不起作用。你能帮忙吗?
<a href="#img1">
<img id="largeImage1" src="google-1.png" />
</a>
<a href="#" class="lightbox" id="img1">
<span></span>
</a>
<div id="thumbs1">
<img src="google-1.png" />
<img src="google-2.png" />
<img src="google-3.png" />
<img src="google-4.png" />
</div>
$('#thumbs1').delegate('img', 'click', function() { <-- This is working fine
$('#largeImage1').attr('src', $(this).attr('src').replace('thumb', 'large')); <-- This is working fine
$('span').css('background-image', 'url()'); <---- This is the problem, I can see in the inspection tool, that when clicked it adds background-image to span but doesn't add image SRC inside it. I don't know what to call inside the url. I checked online but seemed confusing
});
提前致谢
从你的例子中并不能完全清楚你想要做什么。这是一个可能对您有所帮助的示例。
$(function() {
function moveShade(target) {
$("#thumbs1 .shade").css({
width: $(target).width(),
height: $(target).height(),
top: $(target).position().top,
left: $(target).position().left
});
}
$('#thumbs1').delegate('img', 'click', function() {
$('#largeImage1').attr('src', $(this).attr('src'));
$('.lightbox span').css('background-image', 'url(' + $(this).attr('src') + ')');
$("#thumbs1 .active").removeClass("active");
$(this).addClass("active");
moveShade(".active");
});
moveShade(".active");
});
a[href='#img1'] img {
width: 480px;
}
#thumbs1 {
position: relative;
}
#thumbs1 img {
width: 100px;
}
#thumbs1 .shade {
background-color: rgba(0, 0, 0, 0.45);
position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#img1">
<img id="largeImage1" src="https://i.imgur.com/HGKqg1T.jpeg" />
</a>
<a href="#" class="lightbox" id="img1">
<span></span>
</a>
<div id="thumbs1">
<img class="active" src="https://i.imgur.com/HGKqg1T.jpeg" />
<img src="https://i.imgur.com/bLvEb3m.jpeg" />
<img src="https://i.imgur.com/IjT9NLy.jpeg" />
<img src="https://i.imgur.com/CkIsdeq.jpeg" />
<div class="shade"></div>
</div>
您可以看到,在 CSS 中,您更新了 url()
,但您必须添加您想要的 URL 字符串。我想这将来自其他缩略图之一。
如何在每次单击#thumbs1 时将#largeImage1 SRC 属性添加到跨度背景图像中?它应该使用新的 SRC 更新跨度背景图像。
我似乎无法让它工作。我的语法不起作用。你能帮忙吗?
<a href="#img1">
<img id="largeImage1" src="google-1.png" />
</a>
<a href="#" class="lightbox" id="img1">
<span></span>
</a>
<div id="thumbs1">
<img src="google-1.png" />
<img src="google-2.png" />
<img src="google-3.png" />
<img src="google-4.png" />
</div>
$('#thumbs1').delegate('img', 'click', function() { <-- This is working fine
$('#largeImage1').attr('src', $(this).attr('src').replace('thumb', 'large')); <-- This is working fine
$('span').css('background-image', 'url()'); <---- This is the problem, I can see in the inspection tool, that when clicked it adds background-image to span but doesn't add image SRC inside it. I don't know what to call inside the url. I checked online but seemed confusing
});
提前致谢
从你的例子中并不能完全清楚你想要做什么。这是一个可能对您有所帮助的示例。
$(function() {
function moveShade(target) {
$("#thumbs1 .shade").css({
width: $(target).width(),
height: $(target).height(),
top: $(target).position().top,
left: $(target).position().left
});
}
$('#thumbs1').delegate('img', 'click', function() {
$('#largeImage1').attr('src', $(this).attr('src'));
$('.lightbox span').css('background-image', 'url(' + $(this).attr('src') + ')');
$("#thumbs1 .active").removeClass("active");
$(this).addClass("active");
moveShade(".active");
});
moveShade(".active");
});
a[href='#img1'] img {
width: 480px;
}
#thumbs1 {
position: relative;
}
#thumbs1 img {
width: 100px;
}
#thumbs1 .shade {
background-color: rgba(0, 0, 0, 0.45);
position: absolute;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#img1">
<img id="largeImage1" src="https://i.imgur.com/HGKqg1T.jpeg" />
</a>
<a href="#" class="lightbox" id="img1">
<span></span>
</a>
<div id="thumbs1">
<img class="active" src="https://i.imgur.com/HGKqg1T.jpeg" />
<img src="https://i.imgur.com/bLvEb3m.jpeg" />
<img src="https://i.imgur.com/IjT9NLy.jpeg" />
<img src="https://i.imgur.com/CkIsdeq.jpeg" />
<div class="shade"></div>
</div>
您可以看到,在 CSS 中,您更新了 url()
,但您必须添加您想要的 URL 字符串。我想这将来自其他缩略图之一。