【问题标题】:Clickable overlapping div(s) and focus可点击的重叠 div 和焦点
【发布时间】:2015-03-30 23:51:50
【问题描述】:

我有一组不同不透明度的多个圆圈,每个圆圈都应该是可点击的,当点击时,应该成为焦点,点击的会出现在其他圆圈的前面。

你会说简单...

我的问题是使用 CSS 或 Jquery 以及如何实现它

例子:

.circles{}
.round{opacity: 0.4;;width:75px;height:75px;border-radius: 50%; position: relative;}
.circle1{background:#0f4977;top: 35px;left: 200px;z-index: 1;}
.circle2{background:#f41875;top: 0px;left: 250px;z-index: 2;}
.circle3{background:#6b259c;top: -50px;left: 205px;z-index: 3;}`


$(".round").click(function() {   //on click of any circle
    $(this).css("z-index", "99");
    $(this).css("opacity", "1");
    $(this).siblings().css("opacity", "0.5");
    $(this).siblings().css("z-index", "5");
});

<div clas="circles">
      <div class="round circle1"></div>
      <div class="round circle2"></div>
      <div class="round circle3"></div>
</div>

小提琴:https://jsfiddle.net/cLqqfh4v/8/

【问题讨论】:

  • 您的问题是什么,您目前拥有的代码在哪里?我不确定您的问题是否会受到好评,因为 SO 不是代码编写服务。请提供您的代码和您尝试过的内容并描述您的代码遇到的问题
  • 这段代码在你的本地不工作吗?小提琴缺少 jquery,但代码看起来不错

标签: html css opacity overlap


【解决方案1】:

比如...

$(".round").click(function() {   //on click of any circle
    $(this).css("z-index", "99"); //set this circle's z-index to 99, i.e. bringing it to the front
    $(this).css("opacity", "1");  //Set its opacity to 1 (opaque)
//Reset all other circles:
    $(this).siblings().css("opacity", "0.5");
    $(this).siblings().css("z-index", "5");
});

当你的 HTML 是:

<div class="round" id="circle1></div>
<div class="round" id="circle2></div>
<div class="round" id="circle3></div>

或类似的东西;你的圈子只需要上课circle

参见 JSFiddle:https://jsfiddle.net/jofish999/somad48j/1/

编辑


正如 @Sai 所述,更有效的解决方案是为 .opaque 类设置 CSS 规则。然后,不是使用 jQuery 来更改圆圈的 CSS,而是使用 addClass() 给它那个类,然后 removeClass() 再次删除它。

$(".round").click(function () { //on click of any circle
   $(this).addClass("opaque");
   $(this).siblings().removeClass("opaque");

你的 CSS 在哪里:

.opaque {opacity:1; z-index:99;}

【讨论】:

  • 感谢 Jofish,这是我的想法,但不起作用。看看我更新的 JSFIddle
  • 知道了。我会看看@wpsupprt
  • 您只需要在左侧添加 jQuery。请参阅我的 JSFiddle @wpsupprt
  • @Jofish -- 很好的解决方案。不过只是一个小小的改进。而不是在 jquery 中使用 css 更改(效率不高)使用添加/删除类 API 来提高效率小提琴:jsfiddle.net/svaidhyanath/somad48j/2
  • 很公平,我确实想到了这一点,但不想过多地弄乱他/她的代码。但好地方,我会补充一点。 @Sai
猜你喜欢
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
  • 2012-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多