javascript - jQuery Image Slices -
i've image slider, in want slice image create kind of 3d-effekt. created slider, i'm struggling responsive behavior of slider. in px values, working finde, want slider responsive. @ code , give me solution solve problem? here code:
the scss:
/*variables & helper*/ *{ box-sizing: border-box; } %clearfix { &:after { content: ""; display: table; clear: both; } } body, html{ margin: 0; padding: 0; background: url('../img/bg.png'); } img{ max-width: 100%; height: auto; } .article{ width: 80%; margin: 2rem auto; background: #fff; @extend %clearfix; padding: .8rem; } .img__container { float: left; width: 100%; margin: 0; } .slice{ float: left; }
and here javascript:
$(function(){ $( window ).on('resize',function() { calcdimensions(); bgposition(); }); /*variables*/ var $imgcontainer = $('.img__container'), $img = $imgcontainer.children('img'), slices = 5, percentage = 100 / slices + '%', imgwidth = $imgcontainer.width(), imgheight = $imgcontainer.height(); /**/ sliceimg(); /*functions*/ function calcdimensions(){ imgwidth = $imgcontainer.width(); imgheight = $imgcontainer.height(); }; function bgposition(){ (var = 0; < slices; i++){ $('#slice' + i).css({ 'background-position' : - ( (imgwidth / slices) * ) + 'px 0%', }); }; } function sliceimg(){ var imglink = $img.attr('src'); /* delete image */ $img.remove(); /*loop*/ /*create new divs*/ (var = 0; < slices; i++){ var newdiv = $('<div></div>').addclass('slice').attr('id', 'slice' + i); $imgcontainer.append(newdiv); }; /**/ bgposition(); $('.slice').css({ 'background-image' : 'url(' + imglink + ')', 'width' : percentage, 'height' : imgheight , 'background-size' : 'cover', }); }; });
so, if resize window, it's kind of responsive. not great real responsive experience. tried that, should right mathematic operation. in jquery can't mathematic operations %, or wrong? because i'm getting errors....
$('.slice').css({ 'background-image' : 'url(' + imglink + ')', 'width' : (imgwidth / slices / 100 * 1%) , 'height' : imgheight , 'background-size' : 'cover', });
i'm looking forward solution. thanks!
best regards,
you said used 'px', think go if use 'em' instead of 'px'. , correct me if wrong, think might want use dootstrap responsive design.
Comments
Post a Comment