【问题标题】:CSS select previous sibling [duplicate]CSS选择上一个兄弟[重复]
【发布时间】:2016-04-02 17:33:57
【问题描述】:

我有一个有两个孩子(部分)的进度条。每当这些子项中的每一个悬停时,进度及其子项的总高度都会发生变化。我已经设法使用next sibling selector 解决了第一个孩子的问题,但我找不到第二个孩子的解决方案(黄色部分)。到目前为止,我已经使用 jQuery 解决了这个问题,但我想用纯 CSS 来解决这个问题。

小提琴:https://jsfiddle.net/zfh263r6/5/

$('#start').on({
  mouseenter: function () {
	  //$(this).css('height', '4px');
      //$( 'progress' ).css('height', '4px');
  },
  mouseleave: function () {
	  //$(this).css('height', '');
     // $( 'progress' ).css('height', '');
  }
});
#progress_wrap {
    position: relative;
    height: 4px; /*max height of progress*/
    background: #f3f3f3;
}

progress {
    appearance: none;
    -moz-appearance: none;
    -webkit-appearance: none;
    width: 100%;
    border:none;
    height: 2px;
    transition:all .25s ease-in;
    cursor: pointer;
    background-color: #fff;
    position: absolute;
    top: 0;
    left: 0;
    display: block;
}

progress:hover, progress:hover + #start {height: 4px}

progress[value]  {
    /* Reset the default appearance */
    -webkit-appearance: none;
     -moz-appearance: none;
    	  appearance: none;

    /* Get rid of default border in Firefox. */
    border: none;

    /* For IE10 */
    color: #f8008c;
}

progress[value]::-webkit-progress-bar {
    background-color: #fff;
    border:none;
}

progress[value]::-webkit-progress-value {background:#f8008c}

progress::-ms-progress-value {background:#f8008c}

progress::-moz-progress-bar {background:#f8008c}

progress::-ms-fill {border: none}

#start {
    height: 2px;
    transition: all .25s ease-in;
    cursor: pointer;
    background-color: #ffe232;
    position: absolute;
    top: 0;
    left: 0px;
    width: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="progress_wrap">
  <progress min="0" max="1" value="0.66" id="progress"></progress> 
  <div id="start" style="display: inline-block; left: 50px;"></div>
</div>

【问题讨论】:

  • CSS 不能选择以前的兄弟姐妹。
  • 有没有其他方法可以仅使用 CSS 来实现这一点? @Vohuman
  • 我不这么认为。我唯一能想到的就是将progressstart 设置为悬停高度,然后使progress_wrap overflow:hidden 更短,并使that 在悬停时扩展。
  • 嘿,这太酷了。它的工作原理谢谢。 @Draco18s
  • 唯一的问题是它将我的其他元素向下推:/

标签: javascript jquery html css


【解决方案1】:

没有 CSS 没有以前的同级选择器,但您可以使用 ~ 选择器 -

假设您有一个链接列表,当悬停在其中一个时,之前的所有链接都应该变成红色。你可以这样做:

/* default link color is blue */
.parent a {
  color: blue;
}

/* prev siblings should be red */
.parent:hover a {
  color: red;
}
.parent a:hover,
.parent a:hover ~ a {
  color: blue;
}
<div class="parent">
  <a href="#">link</a>
  <a href="#">link</a>
  <a href="#">link</a>
  <a href="#">link</a>
  <a href="#">link</a>
</div>

【讨论】:

  • 感谢您的解决方案!
【解决方案2】:

来自评论:

使progressstart 成为悬停高度,然后使progress_wrapoverflow:hidden 和更短,并使 在悬停时展开。

为了避免推动其他元素,我们可以同时添加一些负下边距。

#progress_wrap {
    position: relative;
    height: 2px;
    background: #f3f3f3;
    overflow:hidden;
    transition: all .25s ease-in;
}

#progress_wrap:hover, progress:hover + #start {
  height: 4px;
  margin-bottom:-2px;
}

progress,#start {
  height: 4px; /*This is a change from existing, not a new declaration*/
}

https://jsfiddle.net/5t90s4jk/

【讨论】:

  • 嘿,对不起,但另一个答案有一个更清洁的解决方案。
  • 没有受伤的感觉。 :)
猜你喜欢
  • 2014-02-09
  • 2012-08-02
  • 2016-07-28
  • 2023-03-15
  • 2020-12-28
  • 1970-01-01
  • 2017-05-20
  • 2019-08-10
  • 1970-01-01
相关资源
最近更新 更多