无法理解CSS选择器在juizy幻灯片样式表中的含义

问题描述:

我试图将juizy slideshow添加到我的网站页面,但我需要完全理解它是如何制作的,以便将其动画(滑动)与我自己的习惯动画我想要添加到页面以及。无法理解CSS选择器在juizy幻灯片样式表中的含义

除了第一个链接中的简要技术说明外,还有更多详细信息(但法语和* 1的可下载代码与讨论的代码略有不同,因为作者对可下载版本做了一些改进) 。

<body> 

    <span id="sl_play" class="sl_command">&nbsp;</span> 
    <span id="sl_pause" class="sl_command">&nbsp;</span> 
    <span id="sl_i1" class="sl_command sl_i">&nbsp;</span> 
    <span id="sl_i2" class="sl_command sl_i">&nbsp;</span> 
    <span id="sl_i3" class="sl_command sl_i">&nbsp;</span> 
    <span id="sl_i4" class="sl_command sl_i">&nbsp;</span> 

    <section id="slideshow"> 

     <a class="commands prev commands1" href="#sl_i4" title="Go to last slide">&lt;</a> 
     <a class="commands next commands1" href="#sl_i2" title="Go to 2nd slide">&gt;</a> 
     <a class="commands prev commands2" href="#sl_i1" title="Go to 1rst slide">&lt;</a> 
     <a class="commands next commands2" href="#sl_i3" title="Go to 3rd slide">&gt;</a> 
     <a class="commands prev commands3" href="#sl_i2" title="Go to 2nd slide">&lt;</a> 
     <a class="commands next commands3" href="#sl_i4" title="Go to 4th slide">&gt;</a> 
     <a class="commands prev commands4" href="#sl_i3" title="Go to 3rd slide">&lt;</a> 
     <a class="commands next commands4" href="#sl_i1" title="Go to first slide">&gt;</a> 

     <a class="play_commands pause" href="#sl_pause" title="Maintain paused">Pause</a> 
     <a class="play_commands play" href="#sl_play" title="Play the animation">Play</a> 

     <div class="container"> 
      <div class="c_slider"></div> 
      <div class="slider"> 
       <figure> 
        <img src="img/dummy-640x310-1.jpg" alt="" width="640" height="310" /> 
        <figcaption>The mirror of soul</figcaption> 
       </figure><!-- 
       --><figure> 
        <img src="img/dummy-640x310-2.jpg" alt="" width="640" height="310" /> 
        <figcaption>Let's cross that bridge when we come to it</figcaption> 
       </figure><!-- 
       --><figure> 
        <img src="img/dummy-640x310-3.jpg" alt="" width="640" height="310" /> 
        <figcaption>Sushi<em>(do)</em> time</figcaption> 
       </figure><!-- 
       --><figure> 
        <img src="img/dummy-640x310-4.jpg" alt="" width="640" height="310" /> 
        <figcaption>Waking Life</figcaption> 
       </figure> 
      </div> 
     </div> 

     <span id="timeline"></span> 

     <ul class="dots_commands"><!-- 
      --><li><a title="Show slide 1" href="#sl_i1">Slide 1</a></li><!-- 
      --><li><a title="Show slide 2" href="#sl_i2">Slide 2</a></li><!-- 
      --><li><a title="Show slide 3" href="#sl_i3">Slide 3</a></li><!-- 
      --><li><a title="Show slide 4" href="#sl_i4">Slide 4</a></li> 
     </ul> 

    </section> 

</body> 

这是它的样式代码:

#slideshow { 
    position: relative; 
    width: 640px; 
    height: 310px; 
    padding: 15px; 
    border: 1px solid #ddd; 
    margin: 0 auto 2em; 
    background: #FFF; 
    background: -webkit-linear-gradient(#FFF, #FFF 20%, #EEE 80%, #DDD); 
    background: -moz-linear-gradient(#FFF, #FFF 20%, #EEE 80%, #DDD); 
    background: -ms-linear-gradient(#FFF, #FFF 20%, #EEE 80%, #DDD); 
    background: -o-linear-gradient(#FFF, #FFF 20%, #EEE 80%, #DDD); 
    background: linear-gradient(#FFF, #FFF 20%, #EEE 80%, #DDD); 

    -webkit-border-radius: 2px 2px 2px 2px; 
    -moz-border-radius: 2px 2px 2px 2px; 
    border-radius: 2px 2px 2px 2px; 

    -webkit-box-shadow: 0 0 3px rgba(0,0,0, 0.2); 
    -moz-box-shadow: 0 0 3px rgba(0,0,0, 0.2); 
    box-shadow: 0 0 3px rgba(0,0,0, 0.2); 
} 

/* Here are some other rules for the #timeline and .dots_commands that i removed to make shorter this code */ 

/* play/pause commands */ 
.play_commands { 
    position: absolute; 
    width: 22px; height: 22px; 
    top: 25px; right: 25px; 
    z-index: 10; 
    text-indent: -9999px; 
    border:0 none; 
    opacity: 0; 

    -webkit-transition: opacity 1s, right 1s; 
    -moz-transition: opacity 1s, right 1s; 
    transition: opacity 1s, right 1s; 
} 
.play { right: 55px; cursor: default; } 

.pause:hover { border:0 none; } 
.play_commands:focus { outline:0; } 

#slideshow:hover .pause, 
#sl_play:target ~ #slideshow:hover .pause, 
.play_commands:focus { 
    opacity: 1; 
} 
.sl_command:target ~ #slideshow:hover .pause, 
#sl_pause:target ~ #slideshow:hover .pause { 
    opacity: 0; 
} 
.pause:after, 
.pause:before { 
    position: absolute; 
    display: block; 
    content: " "; 
    top:0; 
    width:38%; 
    height: 22px; 
    background: #fff; 
    background: rgba(255,255,255,0.5); 
} 
.pause:after { 
    right:0; 
} 
.pause:before { 
    left:0; 
} 
.play { 
    width: 1px; 
    height: 1px; 
    border-top: 10px solid transparent; 
    border-left: 20px solid #fff; 
    border-left: 20px solid rgba(255,255,255,0.5); 
    border-bottom: 10px solid transparent; 
    opacity: 0; 
} 
.play:hover, 
.play:focus { 
    border-bottom: 10px solid transparent; 
} 

#slideshow .container { 
    position:relative; 
    width: 640px; 
    height: 310px; 
    overflow: hidden; 
} 
/* timeline base */ 
#slideshow .container:after { 
    position:absolute; 
    bottom: 0; left:0; 
    content: " "; 
    background: #999; 
    width: 100%; 
    height: 1px; 
} 

@-webkit-keyframes slider { 
    0%, 20%, 100% { left: 0 } 
    25%, 45%  { left: -100% } 
    50%, 70%  { left: -200% } 
    75%, 95%  { left: -300% } 
} 
@-moz-keyframes slider { 
    0%, 20%, 100% { left: 0 } 
    25%, 45%  { left: -100% } 
    50%, 70%  { left: -200% } 
    75%, 95%  { left: -300% } 
} 
@keyframes slider { 
    0%, 20%, 100% { left: 0 } 
    25%, 45%  { left: -100% } 
    50%, 70%  { left: -200% } 
    75%, 95%  { left: -300% } 
} 

#slideshow .slider { 
    position: absolute; 
    left:0; top:0; 
    width: 400%; 
    height: 310px; 

    -webkit-animation: slider 32s infinite; 
    -moz-animation: slider 32s infinite; 
    animation: slider 32s infinite; 
} 
.sl_i:target ~ #slideshow .slider { 
    -webkit-transition: left 1s; 
    -moz-transition: left 1s; 
    transition: left 1s; 
} 
.sl_command:target ~ #slideshow .slider { 
    -webkit-transition: opacity 1s; 
    -moz-transition: opacity 1s; 
    transition: opacity 1s; 
} 
#slideshow .c_slider { 
    position: absolute; 
    left:0; top:0; 
    width: 400%; 
    height: 310px; 
    background: url(img/dummy-640x310-1.jpg) 0 0 no-repeat, url(img/dummy-640x310-2.jpg) 640px 0 no-repeat, url(img/dummy-640x310-3.jpg) 1280px 0 no-repeat, url(img/dummy-640x310-4.jpg) 1920px 0 no-repeat; 
} 
.sl_i:target ~ #slideshow .c_slider { 
    -webkit-transition: background 1s; 
    -moz-transition: background 1s; 
    transition: background 1s; 
} 


/* need a stop ! */ 

/* actions when target ! */ 
.sl_command { display: none; } 

.sl_command:target ~ #slideshow .slider, 
.sl_command:target ~ #slideshow figure:after, 
.sl_command:target ~ #slideshow figcaption, 
.sl_command:target ~ #slideshow #timeline, 
.sl_command:target ~ #slideshow .dots_commands li:first-child a:after { 
    -webkit-animation-play-state: paused; 
    -moz-animation-play-state: paused; 
    animation-play-state: paused; 
} 

#sl_play:target ~ #slideshow .slider, 
#sl_play:target ~ #slideshow figure:after, 
#sl_play:target ~ #slideshow figcaption, 
#sl_play:target ~ #slideshow #timeline, 
#sl_play:target ~ #slideshow .dots_commands li:first-child a:after { 
    -webkit-animation-play-state: running; 
    -moz-animation-play-state: running; 
    animation-play-state: running; 
} 

.sl_command:target ~ #slideshow .pause { opacity:0; } 
.sl_command:target ~ #slideshow .play { opacity:1; right: 25px; cursor: pointer; } 
#sl_play:target ~ #slideshow .pause  { opacity:0; } 
#sl_play:target ~ #slideshow .play  { opacity:0; right: 55px; cursor: default;} 

.sl_i:target ~ #slideshow .slider         { visibility: hidden } 
.sl_i:target ~ #slideshow .slider figcaption      { visibility: hidden } 
.sl_i:target ~ #slideshow .dots_commands li:first-child a:after  { display:none; } 
.sl_i:target ~ #slideshow .dots_commands li:first-child a:before { display:block; }} 

#sl_i1:target ~ #slideshow .commands        { display: none; } 
#sl_i1:target ~ #slideshow .commands1        { display: block; } 
#sl_i1:target ~ #slideshow .c_slider        { background-position: 0 0, 640px 0, 1280px 0, 1920px 0; } 
#sl_i1:target ~ #slideshow .dots_commands li:first-child a:before { left:0; } 

#sl_i2:target ~ #slideshow .commands        { display: none; } 
#sl_i2:target ~ #slideshow .commands2        { display: block; } 
#sl_i2:target ~ #slideshow .c_slider        { background-position: -640px 0, 0 0, 640px 0, 1280px 0; } 
#sl_i2:target ~ #slideshow .dots_commands li:first-child a:before { left:18px; } 

#sl_i3:target ~ #slideshow .commands        { display: none; } 
#sl_i3:target ~ #slideshow .commands3        { display: block; } 
#sl_i3:target ~ #slideshow .c_slider        { background-position: -1280px 0, -640px 0, 0 0, 640px 0; } 
#sl_i3:target ~ #slideshow .dots_commands li:first-child a:before { left:36px; } 

#sl_i4:target ~ #slideshow .commands        { display: none; } 
#sl_i4:target ~ #slideshow .commands4        { display: block; } 
#sl_i4:target ~ #slideshow .c_slider        { background-position: -1920px 0, -1280px 0, -640px 0, 0 0; } 
#sl_i4:target ~ #slideshow .dots_commands li:first-child a:before { left:54px; } 

选择我无法从以前的样式表理解的组合,就在这条规则:

#slideshow:hover .pause, 
#sl_play:target ~ #slideshow:hover .pause, 
.play_commands:focus { 
    opacity: 1; 
} 
  1. 第一个组合#slideshow:hover .pause是否转换为“选择所有类名暂停为de的元素元素的幻灯片ID的元素“?

  2. #sl_play:target ~ #slideshow:hover .pause表示什么?

------------------编辑后Brian Stephens answer --------------------- -

对于问题2)我仍然无法理解什么是悬停状态必须由具有:target伪类的ID前面的那个意思,一般在CSS的优先级为约元件配置在代码,但在这种情况下,它看起来像是关于事件而已:是否意味着必须首先激活指定#sl_play的超链接,然后在#slideshow上进行悬停规则可能有效?如果优先级是关于事件的,那么代码中的优先级就不再重要了?

问题1:请问第一组合#slideshow:hover .pause翻译成“选择与类名可以暂停该元素的后裔与幻灯片的ID的所有元素”?

答案:是的,它是一个后代选择器,它没有指定它必须是一个孩子;它可能会进一步下降。请记住,它也指定悬停状态。

问题#2#sl_play:target ~ #slideshow:hover .pause表示什么?

答案:这使用一个通用的兄弟连接器。 #slideshow:hover之前必须有#sl_play:target,但不一定立即。它必须是后来的兄弟姐妹。

这里有一个很好的文章,解释孩子的一切变化和同胞选择:CSS Tricks

+0

对于问题2我仍然无法理解的是,一个悬停状态的必须由具有一个ID前面的含义:目标伪类,通常在CSS中,precedance是关于代码中的元素处置,但在这种情况下,它看起来像是关于事件:是否意味着必须首先激活针对#sl_play的超链接,然后悬停应该发生在该规则可能会失败的#slideshow?我不这么认为,但为什么不立即使用#sl_play:target? – Bardelman

+1

@Bardelman:他们是完全独立的。选择器简单地期望一个元素应该匹配':target',而另一个元素应该匹配':hover'。如果这些元素中的任何一个与选择器给出的伪类不匹配,则选择器将不匹配。作为另一个例子,它就像'#id.class1〜.pause' - 如果元素没有“class1”,那么选择器就不会匹配。 – BoltClock

+0

@Bardelman:你为什么要同时使用?这完全取决于CSS如何工作。也许它只有在这两个条件中的每一个以特定顺序满足时才有效。 – BoltClock