【问题标题】:Make icons pop out only on icon hover not div使图标仅在图标悬停时弹出而不是 div
【发布时间】:2018-07-13 13:07:32
【问题描述】:

嗨,我正在尝试制作一个弹出菜单,所以当主图标悬停或单击时,如果需要,其他图标会左右滑出。但目前,当您将鼠标悬停在 div 容器中的任意位置时,它会弹出,而不仅仅是主图标(在本例中为图标 3)。

我不介意必须使用脚本,但即使那样也无法隔离它。

代码如下:

小提琴:https://jsfiddle.net/oxe6jg1L/2/

html:

<div class="social-icons">
            <div class="social-icons-image">
                <a href="">
                    <img src="https://www.facebook.com/images/fb_icon_325x325.png" alt="test">
                </a>
            </div>
            <div class="social-icons-image">
                <a href="">
                    <img src="https://www.facebook.com/images/fb_icon_325x325.png" alt="test">
                </a>
            </div>
            <div class="social-icons-image">
                <a href="">
                    <img src="https://www.facebook.com/images/fb_icon_325x325.png" alt="Test">
                </a>
            </div>
            <div class="social-icons-image">
                <a href="https://plus.google.com">
                    <img src="https://www.facebook.com/images/fb_icon_325x325.png" alt="test">
                </a>
            </div>
            <div class="social-icons-image">
                <a href="">
                    <img src="https://www.facebook.com/images/fb_icon_325x325.png" alt="Linkedin Icon">
                </a>
            </div>
        </div>
</div>

css:

.social-icons {
    margin: 0 auto;
    width: 640px;
    height: 128px;
    position: relative;
}


.social-icons .social-icons-image {
    display: inline-block;
    position: absolute;
    width: 33%;
    height: auto;
    z-index: 2;
    opacity: 1;
    transition: all .5s;
    padding: 2%;
    box-sizing: border-box;
}

.social-icons .social-icons-image a {
    display: inline-block;
    width: 100%;
    height: 100%;
}


.social-icons img {
    width: 100%;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.social-icons a:hover img {
    width: 110%;
    height: auto;
    margin: -5%;
}

.social-icons .social-icons-image:nth-child(1) {
    left: 33.755%;   /*(nth-child(2).left - (50% * 20%)/4)*/
    top: 25%; /*((100%-50%)/2)*/
    z-index: 0;
    width: 10%; /*(50% * 20%)*/
    height: auto;
    opacity: .5;
}


.social-icons .social-icons-image:nth-child(2) {
    left: 36.25%;   /*(40% - (75% * 20%)/4)*/
    top: 12.5%; /*((100%-75%)/2)*/
    z-index: 1;
    width: 15%; /*(75% * 20%)*/
    height: auto;
    opacity: .75;
}

.social-icons .social-icons-image:nth-child(3) {
    left: 40%;
    z-index: 2;
    width: 20%;
    height: auto;
}

.social-icons .social-icons-image:nth-child(4) {
    left: 48.75%; /*(60% - 3*(75% * 20%)/4*/
    top: 12.5%; /*((100%-75%)/2)*/
    z-index: 1;
    width: 15%; /*(75% * 20%)*/
    height: auto;
    opacity: .75;
}

.social-icons .social-icons-image:nth-child(5) {
    left: 56.25%;   /*(nth-child(4).left + (nth-child(4).width- 3*(50% * 20%)/4)*/
    top: 25%; /*((100%-50%)/2)*/
    z-index: 0;
    width: 10%; /*(50% * 20%)*/
    height: auto;
    opacity: .5;
}

.social-icons:hover .social-icons-image:nth-child(1) {
    top: 0px;
    left: 0%;
    width: 20%;
    opacity: 1;
}

.social-icons:hover .social-icons-image:nth-child(2) {
    top: 0px;
    left: 20%;
    width: 20%;
    opacity: 1;
}

.social-icons:hover .social-icons-image:nth-child(4) {
    top: 0px;
    left: 60%;
    width: 20%;
    opacity: 1;
}

.social-icons:hover .social-icons-image:nth-child(5) {
    top: 0px;
    left: 80%;
    width: 20%;
    opacity: 1;
}

【问题讨论】:

  • 没有javascript,因此没有jquery,你为什么用这两个标签标记这个问题?
  • 答案是,因为.social-icons:hover

标签: css


【解决方案1】:

您在 social-icons 类上添加了悬停属性,这就是它在 div 上工作的原因。我在social-icons div 中又添加了一个内部div,并在该div 上添加了所有属性。新的 div 类是social-icons-inner

.social-icons {
    margin: 0 auto;
    width: 640px;
    height: 128px;
    position: relative;
}


.social-icons .social-icons-image {
    display: inline-block;
    position: absolute;
    width: 33%;
    height: auto;
    z-index: 2;
    opacity: 1;
    transition: all .5s;
    padding: 2%;
    box-sizing: border-box;
}

.social-icons .social-icons-image a {
    display: inline-block;
    width: 100%;
    height: 100%;
}


.social-icons img {
    width: 100%;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.social-icons a:hover img {
    width: 110%;
    height: auto;
    margin: -5%;
}

.social-icons .social-icons-image:nth-child(1) {
    left: 33.755%;   /*(nth-child(2).left - (50% * 20%)/4)*/
    top: 25%; /*((100%-50%)/2)*/
    z-index: 0;
    width: 10%; /*(50% * 20%)*/
    height: auto;
    opacity: .5;
}


.social-icons .social-icons-image:nth-child(2) {
    left: 36.25%;   /*(40% - (75% * 20%)/4)*/
    top: 12.5%; /*((100%-75%)/2)*/
    z-index: 1;
    width: 15%; /*(75% * 20%)*/
    height: auto;
    opacity: .75;
}

.social-icons .social-icons-image:nth-child(3) {
    left: 40%;
    z-index: 2;
    width: 20%;
    height: auto;
}

.social-icons .social-icons-image:nth-child(4) {
    left: 48.75%; /*(60% - 3*(75% * 20%)/4*/
    top: 12.5%; /*((100%-75%)/2)*/
    z-index: 1;
    width: 15%; /*(75% * 20%)*/
    height: auto;
    opacity: .75;
}

.social-icons .social-icons-image:nth-child(5) {
    left: 56.25%;   /*(nth-child(4).left + (nth-child(4).width- 3*(50% * 20%)/4)*/
    top: 25%; /*((100%-50%)/2)*/
    z-index: 0;
    width: 10%; /*(50% * 20%)*/
    height: auto;
    opacity: .5;
}

.social-icons-inner:hover .social-icons-image:nth-child(1) {
    top: 0px;
    left: 0%;
    width: 20%;
    opacity: 1;
}

.social-icons-inner:hover .social-icons-image:nth-child(2) {
    top: 0px;
    left: 20%;
    width: 20%;
    opacity: 1;
}

.social-icons-inner:hover .social-icons-image:nth-child(4) {
    top: 0px;
    left: 60%;
    width: 20%;
    opacity: 1;
}

.social-icons-inner:hover .social-icons-image:nth-child(5) {
    top: 0px;
    left: 80%;
    width: 20%;
    opacity: 1;
}
<div class="social-icons">
  <div class="social-icons-inner">
            <div class="social-icons-image">
                <a href="">
                    <img src="https://www.facebook.com/images/fb_icon_325x325.png" alt="test">
                </a>
            </div>
            <div class="social-icons-image">
                <a href="">
                    <img src="https://www.facebook.com/images/fb_icon_325x325.png" alt="test">
                </a>
            </div>
            <div class="social-icons-image">
                <a href="">
                    <img src="https://www.facebook.com/images/fb_icon_325x325.png" alt="Test">
                </a>
            </div>
            <div class="social-icons-image">
                <a href="https://plus.google.com">
                    <img src="https://www.facebook.com/images/fb_icon_325x325.png" alt="test">
                </a>
            </div>
            <div class="social-icons-image">
                <a href="">
                    <img src="https://www.facebook.com/images/fb_icon_325x325.png" alt="Linkedin Icon">
                </a>
            </div>
        </div>
        </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多