Tha*_*ame 6 html javascript jquery lightbox
我有一个图像,点击后,将打开一个灯箱(来自http://lokeshdhakar.com/projects/lightbox2/的脚本),我想要做的是禁用当按钮切换为关闭时发生.(因此单击图像不会执行任何操作.)
我尝试使用.off和.unbind在网站上的其他一些答案后禁用灯箱,但它们都没有为我工作.我也跟着如何使用jQuery动态启用/禁用链接?如建议但没有运气.
下面是HTML.
<div style="margin-left:10%;padding:1px 16px;">
<section id="four_columns">
<article class="img-item">
<figure>
<a href="img/livingroom.jpg" data-lightbox="livingroom"><img id="img_window1" src="img/livingroom.jpg" width="200" height="120"></a>
<figcaption>
<strong>Living Room
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="window1" value="window1" checked>
<label class="onoffswitch-label" for="window1">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</strong>
</figcaption>
</figure>
</article>
...
Run Code Online (Sandbox Code Playgroud)
和javascript
<script type="text/javascript">
$(document).ready(function() {
var full_opacity = 1;
var faded_opacity = 0.3;
var fade_speed = 'fast';
var objid;
$('input[name="onoffswitch"]').each(function() {
objid = "#img_" + $(this).val();
if($(this).prop('checked')) {
$(objid).css('opacity', full_opacity);
}
else {
$(objid).css('opacity', faded_opacity);
}
});
$('input[name="onoffswitch"]').change(function() {
var objid = "#img_" + $(this).val();
console.log($(this).prop('checked'));
if($(this).prop('checked')) {
$(objid).fadeTo(fade_speed, full_opacity);
}
else {
$(objid).fadeTo(fade_speed, faded_opacity);
$(objid).parent().unbind('click'); /* Here is where I want to implement the code. */
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.
您需要禁用灯箱(通过删除data-lightbox它查找的属性)和底层超链接的默认功能。
$lightboxes = $(".myImageLinks");
// save the old data attributes in an array, then remove them
var lightboxData = $lightboxes.map(function() {
return $(this).data("lightbox");
}).get();
$(objid).parent().removeAttr("data-lightbox");
// prevent the browser from traveling to the link target
var preventer = function(e) {
e.preventDefault();
});
$(objid).parent().click(preventer);
Run Code Online (Sandbox Code Playgroud)
然后使用以下命令重新启用默认单击功能:
$(objid).parent().unbind('click', preventer);
$lightboxes.attr("data-lightbox", function(i, old) {
return lightboxData[i];
});
Run Code Online (Sandbox Code Playgroud)
禁用灯箱的另一个选项是删除“data-lightbox”属性并将其临时保存为“data-oldlightbox”属性。
我觉得图书馆应该使用一个类来识别哪些元素适合灯箱。这对用户(而不仅仅是库)很重要,并且应该进行语义标记。数据属性不适用于 CSS 挂钩。
| 归档时间: |
|
| 查看次数: |
1280 次 |
| 最近记录: |