1

第一次发帖,请多多包涵。

我正在尝试创建一个幻灯片:

  • 为每个分配一个 z-index<section>
  • 设置所有幻灯片的不透明度为 0.7
  • 将当前顶部幻灯片的不透明度指定为 1

这是HTML:

<div id="slides">
  <section class="slide">
    <article>
      <h1>6</h1>
    </article>
  </section>
  <section class="slide">
    <article>
      <h1>5</h1>
    </article>
  </section>
  <section class="slide">
    <article>
      <h1>4</h1>
    </article>
  </section>
  <section class="slide">
    <article>
      <h1>3</h1>
    </article>
  </section>
  <section class="slide">
    <article>
      <h1>2</h1>
    </article>
  </section>
  <section class="slide">
    <article>
      <h1>1</h1>
    </article>
  </section>
</div>
<div id="prev">
  <a href="#previous">&larr;</a>
</div>
<div id="next">
  <a href="#next">&rarr;</a>
</div>

这是到目前为止的JS:

$(document).ready(function() {
  var z = 0;
  var inAnimation = false;

  $('section.slide').each(function() {
    z++;
    $(this).css('z-index', z);
  });

  function swapFirstLast(isFirst) {
    if(inAnimation) return false;
    else inAnimation = true;

    var processZindex, direction, newZindex, inDeCrease;

    if(isFirst) {
      processZindex = z; direction = '-'; newZindex = 1; inDeCrease = 1;
    } else {
      processZindex = 1; direction = ''; newZindex = z; inDeCrease = -1;
    }

    $('section.slide').each(function() {
      if($(this).css('z-index') == processZindex) {
        $(this).animate({ 'top' : direction + $(this).height() + 'px' }, 'slow', function() {
          $(this).css('z-index', newZindex)
          .animate({ 'top' : '0' }, 'slow', function() {
            inAnimation = false;
          });
        });
      } else {
        $(this).animate({ 'top' : '0' }, 'slow', function() {
          $(this).css('z-index', parseInt($(this).css('z-index')) + inDeCrease);
        });
      }

      return false;
    }

    $('#next a').click(function() {
      return swapFirstLast(true);
    });

    $('#prev a').click(function() {
      return swapFirstLast(false);
    });
  });
});

我想我可以在脚本中插入以下代码:

if($(this).css('z-index') == processZindex) {
  $(this).css('opacity', 1);
} else {
  $(this).css('opacity', 0.7);
}

问题是我似乎无法让不透明度为 1 以跟上 z-index 值 6。所以我想写$(this).css('z-index') == processZindexas$(this).css('z-index') != 6但问题发生了。

有没有更简单的编码方法?

4

1 回答 1

2

我只是使用元素的顺序而不是 z-index,使用appendTo, preprendTo

http://jsfiddle.net/jtbowden/RAT8N/1/

我无法决定一个功能或单独的功能是否更好。这是两个单独的函数:

http://jsfiddle.net/jtbowden/5LJcA/3/

这就是你的目的吗?

于 2012-04-17T19:29:04.747 回答