简体   繁体   English

CSS-使用悬停以不同方式影响多个元素

[英]CSS - Using hover to affect multiple elements differently

I'm looking to create something akin to the Mac App Dock. 我正在寻找与Mac App Dock类似的东西。 Basically, I have an number of divs all next to each other with the same class, and a unique ID (done through php). 基本上,我有多个div彼此相邻且具有相同的类,并且具有唯一的ID(通过php完成)。

Currently, when you hover over one element, it magnifies it using transform, however this doesn't affect the elements adjacent to them. 当前,当您将鼠标悬停在一个元素上时,它会使用变换将其放大,但这不会影响与其相邻的元素。

I'm wondering if there's a way to essentially make it so item-2 has a hover effect of transform:scale(2.0), and upon hovering over that element, item-1 & item-3 would get an effect of transform:scale(1.5); 我想知道是否有一种方法可以使item-2具有transform:scale(2.0)的悬浮效果,并且将鼠标悬停在该元素上时,item-1和item-3将具有transform:scale的效果(1.5);

Although, I imagine this is impossible in css. 虽然,我认为这在CSS中是不可能的。 If so, is there a way I can achieve this effect in php or javascript somehow? 如果是这样,有什么办法可以以某种方式在php或javascript中达到这种效果吗?

This is tricky since transform: scale doesn't seem to behave consistently across browsers. 这是棘手的,因为进行了transform: scale在浏览器之间, transform: scale似乎并不一致。

I put together some CSS and Javascript to do what you described, although making it look good on all browsers would take much more time. 我整理了一些CSS和Javascript来完成您描述的操作,尽管要使其在所有浏览器上看起来都不错,将花费更多时间。

Try out my demo here: CodePen 在这里尝试我的演示: CodePen

HTML HTML

<ul id="list">
  <li><a href="#">One</a></li>
  <li><a href="#">Two</a></li>
  <li><a href="#">Three</a></li>
  <li><a href="#">Four</a></li>
  <li><a href="#">Five</a></li>
  <li><a href="#">Six</a></li>
</ul>

CSS CSS

#list li:hover {
  zoom: 2;
  -webkit-transform: scale(2);
  -moz-transform:    scale(2);
  -ms-transform:     scale(2);
  -o-transform:      scale(2);
  transform:         scale(2);
  position: relative;
  z-index: 999;
 }
.beside {
   zoom: 1.5;
  -webkit-transform: scale(1.5);
  -moz-transform:    scale(1.5);
  -ms-transform:     scale(1.5);
  -o-transform:      scale(1.5);
  transform:         scale(1.5);
 }

Javascript (jQuery) Javascript(jQuery)

  $('#list li').on("mouseenter", function() {
    $(this).prev().addClass("beside");
    $(this).next().addClass("beside");
  });
  $('#list li').on("mouseleave", function() {
    $(this).prev().removeClass("beside");
    $(this).next().removeClass("beside");
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM