jquery cycle automatically fit to window width on resize

穿精又带淫゛_ 提交于 2019-12-03 16:13:15

This is how I managed to do it....

$(document).ready(function() {


$('#slideshow').cycle({

slideResize: true,
containerResize: true,
width: '100%',
height: '100%',
fit: 1,

fx: 'fade',
next: '#next',
prev: '#prev',  

});

});

Hope this helps anyone looking to solve this issue (I haven't fully tested it yet, and when I put in the pager button it seems to play up, similarly when using an fx such as scrollHorz it seems to mess it up..)

Actually (as stated in docs), the only thing you have to do is to specify fit option:

$('.slideshow').cycle({
  fit: 1
});

Note that you may need to use width and height options as well - for me that wasn't necessary.
They work only when fit: 1 is set and specify width and height that slideshow should be set to have.

You can add this code to your javascript

$(window).resize(function() {

    var slideDiv = $('.slideshow');

    /* ratio = (width / height) is the aspect ratio of the image. 
       You can change this according to dimensions of the image you are
       using */

    var ratio = (640/426);  // width and height of your image 

    var w = slideDiv.parent().width();
    var h = w / ratio;

    slideDiv.width(w);
    slideDiv.height(h);

    slideDiv.children().each(function() {
        $(this).width(w);  // "this" is the img element inside your slideshow
        $(this).height(h);  // "this" is the img element inside your slideshow
    });

});

I have done this JSFiddle https://jsfiddle.net/0x24u41f/25/ so you can test the above solution.

There is no need to wrap the images with the tag 'div'.

Your html code can be like this:

<div class="slideshow">
    <img src="images/img1.jpg" alt="" />
    <img src="images/img2.jpg" alt="" />
    <img src="images/img3.jpg" alt="" />
</div>
ManMohan Vyas

similar post can be found here JavaScript window resize event

window.onresize = function(event) {
   $('.slideshow').cycle({
     timeout: 400,
     fx: 'scrollHorz',
     next: '#next',
     prev: '#prev',

   });

}

check if that helps

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!