2012-04-17 30 views
1

首次发布,请耐心等待。幻灯片放映增加了Z指数并减少了不透明度

我试图创建幻灯片是:

  • 分配的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') != 6,但问题发生。

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

+0

我有点不清楚你想要什么。你想要幻灯片堆叠?然后用z-index和不透明度来上下拖动?为什么z-index在这里很重要? – 2012-04-17 17:37:17

+0

你的代码似乎有些复杂,正如Jeff B所说,你能更详细地解释你试图达到的效果吗? – trickyzter 2012-04-17 17:45:16

+0

z-index对于我想达到的效果非常重要:幻灯片层位于彼此顶部,幻灯片位于顶部幻灯片稍半透明。就像一大堆煎饼的顶部不透明度为1。 – 2012-04-17 18:46:54

回答

2

我只是用元素,而不是z索引的顺序,使用appendTopreprendTo

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

我不能决定,如果一个函数或独立的功能更好。下面是两个单独的函数:

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

是,你要什么?

+0

我尝试了你建议的代码,并且每当我循环浏览所有内容时,我的幻灯片在浏览器窗口上的压力都会降低。参考资料(书籍,网站)我可以用它来学习如何编写类似于您制作的代码吗?你和trickyzter并不是在开玩笑我的代码错综复杂。 – 2012-04-17 20:29:36

+0

你的意思是他们的偏移量增加了?什么浏览器?这是否在我的例子中,或者当你复制粘贴我的代码? – 2012-04-17 20:31:52

+0

另外,练习练习练习。我通过玩它学习了jQuery,除了jQuery网站和SO答案之外,我没有使用任何引用。 – 2012-04-17 20:32:37