1

我想根据窗口宽度(加载和调整大小)更改 jQuery 选项。

我找到了接近我需要的解决方案,但我对 jQuery 或 javascript 的了解不够,无法根据我的需要定制它们。

这是我的 jQuery 代码:

<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>

我想改变startheight基于窗口宽度。

如果窗口宽度高于 1280,我希望高度的值为 515,如果低于 1280,我希望高度为 615,如果宽度小于 480,则高度为 715。

在另一篇文章的帮助下,可以使用此脚本更改我需要的 css:

$(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);
});

但我还需要即时更改 jQuerystartheight值。

4

2 回答 2

1

为什么不在 CSS 中使用百分比。

我在这里创建了一个示例:

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

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

CSS:

#slider {
    width: 90%; 
}

要调整大小,请在调整大小时使用 JavaScript 和类似的东西:

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

现在在图像上尝试以下操作:

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

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

hth

于 2012-09-03T03:53:26.283 回答
0

像这样修改“加载调整大小”事件绑定:

$(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); }      
});

这适用于大多数 jquery 插件,但由于您似乎使用的是付费插件(我的猜测是 http://codecanyon.net/item/slider-revolution-responsive-jquery-plugin/2580848),自定义可能是受限制 - 所以,一切顺利!:)

于 2012-09-03T08:22:09.077 回答