使图标仅在图标悬停而不在div上弹出

时间:2018-07-13 04:53:06

标签: css

嗨,我正尝试制作一个弹出菜单,因此当悬停或单击主图标(如果需要)时,其他图标将左右滑动。但是目前,当您将鼠标悬停在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;
}

1 个答案:

答案 0 :(得分:1)

您在social-icons类上添加了悬停属性,这就是它在div上起作用的原因。我在div div中添加了另一个内部social-icons,并在该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>