如何使用 jQuery 将背景图像 url 从一个 div 传递到另一个 div?
How do I pass a background-image url from one div onto another using jQuery?
我有一组 div,选中后会打开模式 window。我希望模态 window 的背景图像 url 从选择打开它的 div 传递。
这是 div 和模态 window 的 html :
<div class="cover-item" style="background-image: url('https://d1nexqccu6ll7z.cloudfront.net/_images/s-14-197-61462CCC-uk.png')"></div>
<div class="cover-item" style="background-image: url('https://d1nexqccu6ll7z.cloudfront.net/_images/s-14-1177-64ACF6EA-uk.png')"></div>
<div class="cover-item" style="background-image: url('https://d1nexqccu6ll7z.cloudfront.net/_images/s-14-1195-057710EE-uk.png')"></div>
<div class="modal-window"></div>
CSS 最初用于隐藏模式,然后在调用时显示它:
.modal-window //hide it out the viewport initially {
-webkit-transform: translate(-50%, -200%);
-ms-transform: translate(-50%, -200%);
transform: translate(-50%, -200%);
background-image: url(/* want to dynamically set this */)
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-ms-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
.modal-window-open //reveal it {
-webkit-transform: translate(-50%, 0);
-ms-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}
和 jQuery 我只是在选择 div:
时打开模态 window
$('.cover-item').on('click', function() {
$('.modal-window').toggleClass('modal-window-open');
});
现在如何让特定的 .cover-item
div 在选中时将其背景图像 url 传递给模态 window?
使用 html()
和 this
的组合
$('.cover-item').on('click', function() {
$('.modal-window').html($(this));
$('.modal-window').toggleClass('modal-window-open');
});
Try following code
$('.cover-item').on('click', function() {
var bg = $(this).css('background-image');
$('.modal-window').toggleClass('modal-window-open');
$('.modal-window').css("background-image", bg);
});
我想您需要将 onclick 侦听器添加到 div 本身,并将 div 对象的引用传递给您调用的函数。像这样:
https://d1nexqccu6ll7z.cloudfront.net/_images/s-14-197-61462CCC-uk.png')" onclick ="setBackground(this)">
最后将背景设置为JS中的.cover-item div:
var setBackground = function (divObject) {
$('.modal-window').toggleClass('modal-window-open');
//Dynamically sets BG image
$('.modal-window').css('background-image', divObject.style.backgroundImage);
};
P.S: 虽然还没有测试你的风格。但是上面的示例仅通过设置 div 的高度和宽度就可以正常工作。
我有一组 div,选中后会打开模式 window。我希望模态 window 的背景图像 url 从选择打开它的 div 传递。
这是 div 和模态 window 的 html :
<div class="cover-item" style="background-image: url('https://d1nexqccu6ll7z.cloudfront.net/_images/s-14-197-61462CCC-uk.png')"></div>
<div class="cover-item" style="background-image: url('https://d1nexqccu6ll7z.cloudfront.net/_images/s-14-1177-64ACF6EA-uk.png')"></div>
<div class="cover-item" style="background-image: url('https://d1nexqccu6ll7z.cloudfront.net/_images/s-14-1195-057710EE-uk.png')"></div>
<div class="modal-window"></div>
CSS 最初用于隐藏模式,然后在调用时显示它:
.modal-window //hide it out the viewport initially {
-webkit-transform: translate(-50%, -200%);
-ms-transform: translate(-50%, -200%);
transform: translate(-50%, -200%);
background-image: url(/* want to dynamically set this */)
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-ms-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
.modal-window-open //reveal it {
-webkit-transform: translate(-50%, 0);
-ms-transform: translate(-50%, 0);
transform: translate(-50%, 0);
}
和 jQuery 我只是在选择 div:
时打开模态 window $('.cover-item').on('click', function() {
$('.modal-window').toggleClass('modal-window-open');
});
现在如何让特定的 .cover-item
div 在选中时将其背景图像 url 传递给模态 window?
使用 html()
和 this
$('.cover-item').on('click', function() {
$('.modal-window').html($(this));
$('.modal-window').toggleClass('modal-window-open');
});
Try following code
$('.cover-item').on('click', function() {
var bg = $(this).css('background-image');
$('.modal-window').toggleClass('modal-window-open');
$('.modal-window').css("background-image", bg);
});
我想您需要将 onclick 侦听器添加到 div 本身,并将 div 对象的引用传递给您调用的函数。像这样:
https://d1nexqccu6ll7z.cloudfront.net/_images/s-14-197-61462CCC-uk.png')" onclick ="setBackground(this)">
最后将背景设置为JS中的.cover-item div:
var setBackground = function (divObject) {
$('.modal-window').toggleClass('modal-window-open');
//Dynamically sets BG image
$('.modal-window').css('background-image', divObject.style.backgroundImage);
};
P.S: 虽然还没有测试你的风格。但是上面的示例仅通过设置 div 的高度和宽度就可以正常工作。