javascript - HTML, jQuery, CSS - Width() returning entire page width instead of div on page load? -
so i'm trying have function apply "padding-bottom" in div when page loaded, problem width returns entire document value instead of div value. when use function (that changes between images) function works fine.
$(window).load(function () { $('.photoset').each(function () { $items = $(this).find('img') function get_height(target) { return (($items.eq(target).height() / $(this).width()) * 100) + '%'; } $items.css({ paddingbottom: get_height(0) }); }); }
the css follow:
.photowrapper{ width:60%; margin:auto; } .photoset { position: relative; display: block; margin:auto; overflow:hidden; } .photoset figure{ width: 100%; margin: 0; padding: 0; position: absolute; top: 0; left: 0; overflow: hidden; text-align:center; } .photoset img { opacity: 0; transition:opacity 1.2s; max-width:100%; } .photoset .photoset-show img{ opacity: 1; }
and html
<div class="photowrapper"> <div class="photoset center-block"> <figure class="photoset-show"> <img src="/images/5220a574-77a8-4aa6-aecc-aedf60eed6e2.jpg" /> </figure> <figure class=""> <img src="/images/9e59af1e-de75-4ddb-b091-f816224d9f6e.jpg" /> </figure> <figure class=""> <img src="/images/ad76e5e3-5780-4f22-bcb4-8106fc41161a.jpg" /> </figure> </div> </div>
this function works intended, returns right width value when switching between images:
var showcurrent = function (photoset) { $items = photoset.find('figure'); $images = photoset.find('img'); var counter = photoset.data('counter'); var numitems = $items.length; var itemtoshow = math.abs(counter % numitems); $items.each(function () { $(this).removeclass('photoset-show'); }) function get_height(target) { return (($images.eq(target).height() / photoset.width()) * 100) + '%'; } photoset.animate({ paddingbottom: get_height(itemtoshow) }, 400); $items.eq(itemtoshow).addclass('photoset-show'); };
this
isn't think inside get_height()
. has different context inside function. store reference this
or pass in argument
try
$(window).load(function () { $('.photoset').each(function () { var $photoset = $(this), $items = $photoset.find('img') function get_height(target) { return (($items.eq(target).height() / $photoset.width()) * 100) + '%'; } $items.css({ paddingbottom: get_height(0) }); }); }
Comments
Post a Comment