引导灯箱:显示标题属性中的图像说明
Bootstrap Lightbox: Showing image captions from title attribute
在Javascript和jQuery中仍然很新,所以这个对我来说很难。
我正在使用这个引导灯箱插件:https://github.com/duncanmcdougall/Responsive-Lightbox
尝试在此站点上使用它 (WIP):http://lastdetailwd.com/modernmetalfabricators/furniture.html
我希望使用 A 标签中的标题属性作为此灯箱的标题。关于如何完成此操作的任何线索?
j查询:
(function ($) {
'use strict';
$.fn.lightbox = function (options) {
var opts = {
margin: 50,
nav: true,
blur: true,
minSize: 0
};
var plugin = {
items: [],
lightbox: null,
image: null,
current: null,
locked: false,
selector: "#lightbox",
init: function (items) {
plugin.items = items;
plugin.selector = "lightbox-"+Math.random().toString().replace('.','');
if (!plugin.lightbox) {
$('body').append(
'<div id="lightbox" class='+plugin.selector+' style="display:none;">'+
'<a href="#" class="lightbox-close lightbox-button"></a>' +
'<div class="lightbox-nav">'+
'<a href="#" class="lightbox-previous lightbox-button"></a>' +
'<a href="#" class="lightbox-next lightbox-button"></a>' +
'</div>' +
'</div>'
);
plugin.lightbox = $("."+plugin.selector);
}
if (plugin.items.length > 1 && opts.nav) {
$('.lightbox-nav', plugin.lightbox).show();
} else {
$('.lightbox-nav', plugin.lightbox).hide();
}
plugin.bindEvents();
},
loadImage: function () {
if(opts.blur) {
$("body").addClass("blurred");
}
$("img", plugin.lightbox).remove();
plugin.lightbox.fadeIn('fast').append('<span class="lightbox-loading"></span>');
var img = $('<img src="' + $(plugin.current).attr('href') + '" draggable="false">');
$(img).load(function () {
$('.lightbox-loading').remove();
plugin.lightbox.append(img);
plugin.image = $("img", plugin.lightbox).hide();
plugin.resizeImage();
});
},
resizeImage: function () {
var ratio, wHeight, wWidth, iHeight, iWidth;
wHeight = $(window).height() - opts.margin;
wWidth = $(window).outerWidth(true) - opts.margin;
plugin.image.width('').height('');
iHeight = plugin.image.height();
iWidth = plugin.image.width();
if (iWidth > wWidth) {
ratio = wWidth / iWidth;
iWidth = wWidth;
iHeight = Math.round(iHeight * ratio);
}
if (iHeight > wHeight) {
ratio = wHeight / iHeight;
iHeight = wHeight;
iWidth = Math.round(iWidth * ratio);
}
plugin.image.width(iWidth).height(iHeight).css({
'top': ($(window).height() - plugin.image.outerHeight()) / 2 + 'px',
'left': ($(window).width() - plugin.image.outerWidth()) / 2 + 'px'
}).show();
plugin.locked = false;
},
getCurrentIndex: function () {
return $.inArray(plugin.current, plugin.items);
},
next: function () {
if (plugin.locked) {
return false;
}
plugin.locked = true;
if (plugin.getCurrentIndex() >= plugin.items.length - 1) {
$(plugin.items[0]).click();
} else {
$(plugin.items[plugin.getCurrentIndex() + 1]).click();
}
},
previous: function () {
if (plugin.locked) {
return false;
}
plugin.locked = true;
if (plugin.getCurrentIndex() <= 0) {
$(plugin.items[plugin.items.length - 1]).click();
} else {
$(plugin.items[plugin.getCurrentIndex() - 1]).click();
}
},
bindEvents: function () {
$(plugin.items).click(function (e) {
if(!$("#lightbox").is(":visible") && ($(window).width() < opts.minSize || $(window).height() < opts.minSize)) {
$(this).attr("target", "_blank");
return;
}
var self = $(this)[0];
e.preventDefault();
plugin.current = self;
plugin.loadImage();
// Bind Keyboard Shortcuts
$(document).on('keydown', function (e) {
// Close lightbox with ESC
if (e.keyCode === 27) {
plugin.close();
}
// Go to next image pressing the right key
if (e.keyCode === 39) {
plugin.next();
}
// Go to previous image pressing the left key
if (e.keyCode === 37) {
plugin.previous();
}
});
});
// Add click state on overlay background only
plugin.lightbox.on('click', function (e) {
if (this === e.target) {
plugin.close();
}
});
// Previous click
$(plugin.lightbox).on('click', '.lightbox-previous', function () {
plugin.previous();
return false;
});
// Next click
$(plugin.lightbox).on('click', '.lightbox-next', function () {
plugin.next();
return false;
});
// Close click
$(plugin.lightbox).on('click', '.lightbox-close', function () {
plugin.close();
return false;
});
$(window).resize(function () {
if (!plugin.image) {
return;
}
plugin.resizeImage();
});
},
close: function () {
$(document).off('keydown'); // Unbind all key events each time the lightbox is closed
$(plugin.lightbox).fadeOut('fast');
$('body').removeClass('blurred');
}
};
$.extend(opts, options);
plugin.init(this);
};
})(jQuery);
作者只是更新了它以包含标题。关闭这个。
相关文章:
- 未捕获的类型错误: 无法读取未定义的属性“图像”
- 动态生成的css背景图像属性中的重音字符
- 使用Javascript将图像属性输出到控制台
- 使用javascript为网页中的所有图像添加属性
- HTML图像未加载(自动添加内联css和属性)
- 当我在操作属性中写入页面名称时,文件图像不会上传
- 当图像和链接是 json 对象属性的值时,它们不起作用
- 使用 JQuery 从另一个元素的 id 为图像分配属性
- 调用图像的数据属性
- Wordpress:如何更改图像标签中的源属性
- 使用jQuery通过更改src属性来设置一个图像标签的动画
- 如何在asp.net图像控件中使用OnClick属性来制作图像库
- JQuery每个函数用HTML图像中的属性填充数组
- Jquery-背景图像随select中的数据属性而变化
- 使用angularjs交换图像标签中的ng-src属性
- 使用crossOrigin属性加载图像.在Firefox插件sdk中
- Image属性complete在实际渲染图像之前返回true
- 根据 JSON 更改图像的 src 属性
- 如何制作 AngularJS 指令来更改图像的 src 属性
- 如何使用jquery访问元素'属性图像源