html - :target not working on previous element

标签 html css

我有两张图片

<div class = "main-container">
        <div  class="img" id = "one">
            <a href="#one"><img src="1.jpg" ></a>
        </div>

        <div class="img" id = "two">
            <a href="#two"><img src="2.jpg"></a>
        </div>
</div>

当我点击第一张图片时,第二张图片应该旋转并且它正在工作,但是当点击第二张图片时,第一张图片不旋转,但是它到达了它的初始位置。仅使用了 css:

#one:target ~ #two{
    transform: perspective( 600px ) rotateY( -355deg );
}
#two:target ~ #one{
    transform: perspective( 600px ) rotateY( -355deg );
}

最佳答案

  • General sibling selector :

    The ~ combinator separates two selectors and matches the second element only if it is preceded by the first, and both share a common parent.


#two 前面有 #one,这将起作用:

#one:target ~ #two {}


#one 前面没有 #two,这将起作用:

#two:target ~ #one {}


只需反转 anchor 的 href 属性中的片段标识符值并删除一般的兄弟选择器。

#one:target {
  transform: perspective(600px) rotateY(-355deg);
}
#two:target {
  transform: perspective(600px) rotateY(-355deg);
}
<div class="main-container">
  <div class="img" id="one">
    <a href="#two">
      <img src="http://placehold.it/50x50">
    </a>
  </div>

  <div class="img" id="two">
    <a href="#one">
      <img src="http://placehold.it/50x50">
    </a>
  </div>
</div>

关于html - :target not working on previous element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40641374/

相关文章:

html - 如何水平对齐具有不同大小的div的按钮?

javascript - 向 Javascript/Jquery 添加多个属性

html - Firefox CSS Content 属性未按预期运行

html - 位置 3 div

javascript - HTML 表格行创建

php - 如何修改从sql表中读取的行

internet-explorer-9 - 使用 CSS 在 IE9 上模拟雕刻(文本阴影)

html - 防止 flex 元素的高度扩展以匹配其他 flex 元素

javascript - 文本在 Safari (5.0.5) 中呈现不一致

javascript - 悬停时下拉菜单媒体屏幕未关闭