如何减少对不透明度的影响

时间:2017-02-03 01:02:24

标签: javascript jquery html css

这是我正在使用sly的导航库,我需要降低项目从屏幕中心到屏幕两侧的不透明度,但我不知道如何接近它,类似这样的事情items with opacity reduced 另外,正如您在代码段中看到的那样,设置" itemNav:' forceCentered'",由于某种原因,当滚动时,它会将窗口的两侧与窗口的两侧分开太多,当滚动到两侧时,它应该看起来像这样:code snippet,因为你可以看到物品反弹到两侧。



jQuery(function($) {
  'use strict';

  // -------------------------------------------------------------
  //   Basic Navigation
  // -------------------------------------------------------------
  (function() {
    var $frame = $('#basic');
    var $slidee = $frame.children('ul').eq(0);
    var $wrap = $frame.parent();

    // Call Sly on frame
    $frame.sly({
      horizontal: 1,
      itemNav: 'forceCentered',
      smart: 1,
      activateMiddle: true,
      activateOn: 'click',
      mouseDragging: 1,
      touchDragging: 1,
      releaseSwing: 1,
      startAt: 3,
      scrollBar: $wrap.find('.scrollbar'),
      scrollBy: 1,
      pagesBar: $wrap.find('.pages'),
      activatePageOn: 'click',
      speed: 300,
      elasticBounds: 1,
      easing: 'easeOutExpo',
      dragHandle: 1,
      dynamicHandle: 1,
      clickBar: 1
    });
  }());
});

.wrap {
  position: relative;
 }
.frame {
  overflow: hidden;
}
.clearfix {
  list-style: none;
  margin: 0;
  padding: 0;
  height: 100%;
}
li {
  float: left;
  width: 227px;
  height: 100%;
  margin: 0 1px 0 0;
  padding: 0;
  background: #333;
  color: #ddd;
  text-align: center;
  cursor: pointer;
  opacity: 0.7;
  font-size: 50px;
}
li.active {
  opacity: 1;
}

<div class="wrap">
  <div class="frame" id="basic">
    <ul class="clearfix">
      <li>0</li>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
      <li>8</li>
      <li>9</li>
      <li>10</li>
    </ul>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://darsa.in/sly/examples/js/vendor/plugins.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Sly/1.6.1/sly.min.js"></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

使用active事件。

循环所有子项并添加类以应用不透明度样式。

jQuery(function($) {
  'use strict';

  // -------------------------------------------------------------
  //   Basic Navigation
  // -------------------------------------------------------------
  (function() {
    var $frame = $('#basic');
    var $slidee = $frame.children('ul').eq(0);
    var $wrap = $frame.parent();

    // Call Sly on frame
    $frame.sly({
      horizontal: 1,
      itemNav: 'forceCentered',
      smart: 1,
      activateMiddle: true,
      activateOn: 'click',
      mouseDragging: 1,
      touchDragging: 1,
      releaseSwing: 1,
      startAt: 3,
      scrollBar: $wrap.find('.scrollbar'),
      scrollBy: 1,
      pagesBar: $wrap.find('.pages'),
      activatePageOn: 'click',
      speed: 300,
      elasticBounds: 1,
      easing: 'easeOutExpo',
      dragHandle: 1,
      dynamicHandle: 1,
      clickBar: 1
    });

    $frame.sly('on', 'active', active);
    active(null, 3);
    
    function active(e, idx) {
      $("#ul").find('li').removeClass();
      $("#ul").find('li').each(function(i, elem) {
        setOpacityClass(Math.abs(i - idx), elem);
        if (i == idx) {
          $(elem).addClass('active');
        }
      })
    }

    function setOpacityClass(grad, elem) {
      $(elem).addClass('op-' + grad);
    }

  }());
});
.wrap {
  position: relative;
}
.frame {
  overflow: hidden;
}
.clearfix {
  list-style: none;
  margin: 0;
  padding: 0;
  height: 100%;
}
li {
  float: left;
  width: 227px;
  height: 100%;
  margin: 0 1px 0 0;
  padding: 0;
  background: #333;
  color: #ddd;
  text-align: center;
  cursor: pointer;
  opacity: 0.7;
  font-size: 50px;
}

li.active {
  opacity: 1;
}

li.op-0 {
  opacity: 1;
}
li.op-1 {
  opacity: 0.7;
}
li.op-2 {
  opacity: 0.5;
}
li.op-3 {
  opacity: 0.3;
}
li.op-4 {
  opacity: 0.2;
}
li.op-5 {
  opacity: 0.1;
}
<div class="wrap">
  <div class="frame" id="basic">
    <ul id="ul" class="clearfix">
      <li>0</li>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
      <li>8</li>
      <li>9</li>
      <li>10</li>
    </ul>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://darsa.in/sly/examples/js/vendor/plugins.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Sly/1.6.1/sly.min.js"></script>