javascript - How to pass data-index attribute to the index of photoswipe js? -
in reference photoswipe js : http://photoswipe.com/documentation/getting-started.html
i'm having trouble passing data-index attribute index of photoswipe.
html:
<td> <div class="picture" itemscope="" itemtype="http://schema.org/imagegallery"> <figure style="display:initial;" itemprop="associatedmedia" itemscope="" itemtype="http://schema.org/imageobject"> <a class="picture" href="images/an241_02.jpg" itemprop="contenturl" data-size="2000x1200" data-index="0" data-title="an241 02 55"> <img class="lazy thumbnail " data-original="image_cache/an241_02-cache.jpg" itemprop="thumbnail" alt="image description" src="image_cache/an241_02-cache.jpg" style="display: inline;"> </a> <figcaption itemprop="caption description">description</figcaption> </figure> </div> </td>
js:
var onthumbnailsclick = function(e) { e = e || window.event; e.preventdefault ? e.preventdefault() : e.returnvalue = false; var etarget = e.target || e.srcelement; // find root element of slide var clickedlistitem = closest(etarget, function(el) { return (el.tagname && el.tagname.touppercase() === 'figure'); }); }); if(!clickedlistitem) { return; } // find index of clicked item looping through child nodes // alternatively, may define index via data- attribute <---how? var clickedgallery = clickedlistitem.parentnode, childnodes = clickedlistitem.parentnode.childnodes, numchildnodes = childnodes.length, nodeindex = 0, index; (var = 0; < numchildnodes; i++) { if(childnodes[i].nodetype !== 1) { continue; } if(childnodes[i] === clickedlistitem) { index = nodeindex; break; } nodeindex++; } if(index >= 0) { // open photoswipe if valid index found openphotoswipe( index, clickedgallery ); } return false; };
you can see above "data-index" attribute located in "a" tag , pass index in js.
apologies i'm not familiar js , appreciate here.
if need data-index, have value of attribute clicked element before last "if" statement, pass openphotoswipe init function :)
var newindex = parseint(etarget.parentnode.getattribute("data-index")); if(newindex >= 0) { // open photoswipe if valid index found openphotoswipe( newindex, clickedgallery ); }
Comments
Post a Comment