我有一套div,当被选中时,打开一个模态窗口。我希望模态窗口的背景图像url被传递到从div选择打开它。
下面是div和模态窗口的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时打开模式窗口:
$('.cover-item').on('click', function() {
$('.modal-window').toggleClass('modal-window-open');
});现在如何让特定的.cover-item div在被选中时将其背景图像url传递到模态窗口?
发布于 2016-03-16 05:22:45
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);
});发布于 2016-03-16 05:19:09
使用html()和this的组合
$('.cover-item').on('click', function() {
$('.modal-window').html($(this));
$('.modal-window').toggleClass('modal-window-open');
});发布于 2016-03-16 05:48:06
我猜您需要将onclick侦听器添加到div本身,并将div对象的引用传递给您调用的函数。就像这样:
最后,将背景设置为JS中的. background div:
var setBackground = function (divObject) {
$('.modal-window').toggleClass('modal-window-open');
//Dynamically sets BG image
$('.modal-window').css('background-image', divObject.style.backgroundImage);
};不过,还没有测试过你的风格。但是上面的例子很好,只要设置div的高度和宽度。
https://stackoverflow.com/questions/36027314
复制相似问题