Change jQuery slider option dynamically based on w

2019-08-15 02:56发布

问题:

I would like to change a jQuery option based on window width (on load as well as on resize).

I've found solutions close to what I need, but I don't understand jQuery or javascript enough to customize them for my needs.

Here's my jQuery code:

<script type="text/javascript">
  var tpj = jQuery;

  tpj.noConflict();

  tpj(document).ready(function () {

    if (tpj.fn.cssOriginal != undefined) tpj.fn.css = tpj.fn.cssOriginal;

    tpj('#rev_slider_1_1').show().revolution({
      delay: 5000,
      startwidth: 1920,
      startheight: 515,
      hideThumbs: 200,

      thumbWidth: 100,
      thumbHeight: 50,
      thumbAmount: 4,

      navigationType: "bullet",
      navigationArrows: "verticalcentered",
      navigationStyle: "navbar",

      touchenabled: "on",
      onHoverStop: "off",

      navOffsetHorizontal: 0,
      navOffsetVertical: 20,

      shadow: 0,
      fullWidth: "on"
    });

  }); //ready
</script>

I want to change the startheight based on window width.

If the window width is above 1280 I would like the value for the height to be 515, and if it is below 1280 I would like the height to be 615 and if the width is less than 480 make the height 715.

With help from another post I am able to change the css I need using this script:

$(window).on('load resize', function () {
  var w = $(window).width();
  $("#rev_slider_1_1 #rev_slider_1_1_wrapper")
    .css('max-height', w > 1280 ? 515 : w > 480 ? 615 : 715);
});

But I need to also change the jQuery startheight value on the fly.

回答1:

Why don't you use percentages in CSS.

I created an example here:

http://jsfiddle.net/webwarrior/aLJQm/52/

<div id="slider"></div>

Css:

#slider {
    width: 90%; 
}

to resize, use JavaScript with something like this on your resize:

var clientWidth = jQuery(window).width();
var clientHeight = jQuery(window).height();
jQuery("#rev_slider_1_1 #rev_slider_1_1_wrapper").height(clientHeight/20);

Trying the following now on images:

.slotholder{
    overflow: hidden;
}
.slotholder img{
    width: 110%;
    margin-left: -5%;
}

as on http://jsfiddle.net/webwarrior/wLFEJ/11/

hth



回答2:

Modify the 'load resize' event binding like so:

$(window).on('load resize', function () {
  var w = $(window).width();
  $("#rev_slider_1_1 #rev_slider_1_1_wrapper")
    .css('max-height', w > 1280 ? 515 : w > 480 ? 615 : 715);
  if(w > 1280) { tpj('#rev_slider_1_1').revolution('option', 'startheight', 515);}
  else if(w < 1280 && w > 480) { tpj('#rev_slider_1_1').revolution('option', 'startheight', 615); }
  else { tpj('#rev_slider_1_1').revolution('option', 'startheight', 715); }      
});

This would work for most jquery plugins, but since you seem to be using a paid plugin (http://codecanyon.net/item/slider-revolution-responsive-jquery-plugin/2580848 would be my guess), the customization might be restricted - so, All the Best! :)