Retrieve data-descr attribute using jquery to use in Fancybox thumbnail -
<div class="shirt-thumb"> <a href="../wp-content/uploads/2014/05/icelandic-logo2.png" class="fancybox fancybox-thumb" rel="shirt1" title="shirt"> <img src="../wp-content/uploads/2014/05/icelandic-logo2.png" data-descr="shirt 1 back" /> </a> </div>
above html. trying grab data-descr attribute , add fancybox thumbnail in jquery can apply css style ::after element. need descriptions appear below each thumbnail. i'm understanding, there isn't built in way this, apply title underneath fullsize image?
this how variables thumbnail list items defined. 1 added thumbdescr, other 3 came fancybox:
init: function (opts, obj) { var = this, list, thumbdescr = opts.description, thumbwidth = opts.width, thumbheight = opts.height, thumbsource = opts.source;
below build of thumbnail list item. part loads fine:
for (var n = 0; n < obj.group.length; n++) { list += '<li><a style="width:' + thumbwidth + 'px;height:' + thumbheight + 'px;" data-descr="' + thumbdescr + '" href="javascript:$.fancybox.jumpto(' + n + ');"></a></li>'; }
and these things have done try retrieve description:
this applied function text data-descr instead of retrieving -
description: function (item) { //function obtain description of thumbnail image var descr; if (item.element) { descr = $(item.element).find('img').attr('data-descr'); } if (!descr && item.type === 'image' && item.href) { descr = item.href; } return descr; }
same thing -
description: function (item) { // function obtain description of thumbnail image var descr; if (item.element) { descr = $(item.element).find('img').data("descr"), } return descr; }
and returned "undefined" -
description : f.find('img').data("descr")
...where f defined class .fancybox earlier in jquery.
yep, i'm pretty new @ this, i'm sure there easy fix i'm not sure how find need. please let me know if didn't include enough info.
if "pretty new @ this", wouldn't advise mess original fancybox js files. achieve pretty want within own fancybox custom initialization using fancybox callbacks.
first, bear in mind fancybox thumbnails helpers appended after (fancybox) image shown on screen, in order append description/title (retrieved data-descr
attribute of <img>
tag) may need use aftershow
callback.
also, since there little delay in process, better use settimeout()
function make sure thumbnails have been appended dom.
so try code :
var _descrflag = false; $(".fancybox").fancybox({ helpers: { thumbs: { width: 80, // thumbnails size height: 80 } }, aftershow: function (current) { // validate if added captions if (!_descrflag) { // wait bit settimeout(function () { // each data-descr $(".fancybox").each(function (i) { // wrap data-descr in html element var _datadescr = "<p>" + $(this).find("img").data("descr") + "</p>"; // append current thumb using .eq() $("#fancybox-thumbs").find("li").css({ backgroundcolor: "#fff" // default transparent }).eq(i).append(_datadescr); }); // each // done? set flag true _descrflag = true; }, 100); // adjust time needed }; // if } }); // fancybox
notes :
- we used
_descrflag
variable flag (and validate first in callback) make sure descriptions added once. - notice need append html element (
var _datadescr
); in example used<p>
can use html element need. if
source
not set, fancybox create thumbnails (big) image shown on box. can reduce overhead , page load specifyingsource
of thumbnail, can same<img>
tag instance, :helpers: { thumbs: { width: 80, height: 80, source: function (current) { return current.element.find("img").attr("src"); } } }
see jsfiddle
... , no need mess original js files ;)
Comments
Post a Comment