悬停后变换比例更改位置元素

时间:2019-02-20 19:43:09

标签: css css-animations css-transforms

我尝试使用transform:scale(1.2)来获得图标周围出现圆圈的效果。 transform我添加到了.icon-style:hover::after

现在此代码已被注释,请取消注释此代码以查看我想要得到的内容。

例如,我尝试执行以下操作:click。但是scale会改变位置圆。

我读了此article,但现在不了解如何在CSS中使用transform-origin

演示:JSFiddle

*{
    background-color: black;
}
.pos-footer {
    position: absolute;
    bottom: 20px;
    right: 30px;
}
.icon-social {
    display: flex;
}
.icon-social-pos {
    position: relative;
}
.icon-style {
    position: relative;
    background-repeat: no-repeat;
    background-position: center; 
    width: 30px;
    height: 30px;
    background-color: rgba(255, 255, 255, 0.25);
    padding: 10px;
    border-radius: 50%;
    transition: background-color .3s ease-in-out;
}
.icon-style:hover {
    background-color: rgba(255, 255, 255, 0.7);
}
.icon-style::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 1;
    width: 100%;
    height: 100%;
    border: 5px solid rgba(255, 255, 255, 0.425);
    border-radius: 50%;
    padding: 7px;
    opacity: 0;
    transition: all .4s ease-in-out;
}
.icon-style:hover::after {
    opacity: 1;
    /* transform: scale(1.2); */
}
.icon-github {
    background-image: url("https://img.icons8.com/windows/30/000000/github.png");
    margin-right: 10px;
}
.icon-linkedin {
    background-image: url("https://img.icons8.com/ios-glyphs/22/000000/linkedin-2.png");
    margin-left: 10px;
}
<div class="pos-footer">
    <div class="icon-social">
        <a href="#">
            <div class="icon-social-pos">
                <div class="icon-style icon-github"></div>
            </div>
        </a>
        <a href="#">
            <div class="icon-social-pos">
                <div class="icon-style icon-linkedin"></div>
            </div>
        </a>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

首先,悬停(.icon-style:hover::after)中的转换将覆盖transform: translate(-50%, -50%);。解决该问题的一种方法是也将翻译添加到悬停:

.icon-style:hover::after {
    opacity: 1;
    transform: scale(1.2) translate(-50%, -50%);
}

然后,您必须在transform-origin.icon-style中定义top left;

*{
    background-color: black;
}
.pos-footer {
    position: absolute;
    bottom: 20px;
    right: 30px;
}
.icon-social {
    display: flex;
}
.icon-social-pos {
    position: relative;
}
.icon-style {
    position: relative;
    background-repeat: no-repeat;
    background-position: center; 
    width: 30px;
    height: 30px;
    background-color: rgba(255, 255, 255, 0.25);
    padding: 10px;
    border-radius: 50%;
    transition: background-color .3s ease-in-out;
}
.icon-style:hover {
    background-color: rgba(255, 255, 255, 0.7);
}
.icon-style::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transform-origin: top left;
    z-index: 1;
    width: 100%;
    height: 100%;
    border: 5px solid rgba(255, 255, 255, 0.425);
    border-radius: 50%;
    padding: 7px;
    opacity: 0;
    transition: all .4s ease-in-out;
}
.icon-style:hover::after {
    opacity: 1;
    transform: scale(1.2) translate(-50%, -50%);
}
.icon-github {
    background-image: url("https://img.icons8.com/windows/30/000000/github.png");
    margin-right: 10px;
}
.icon-linkedin {
    background-image: url("https://img.icons8.com/ios-glyphs/22/000000/linkedin-2.png");
    margin-left: 10px;
}
<div class="pos-footer">
    <div class="icon-social">
        <a href="#">
            <div class="icon-social-pos">
                <div class="icon-style icon-github"></div>
            </div>
        </a>
        <a href="#">
            <div class="icon-social-pos">
                <div class="icon-style icon-linkedin"></div>
            </div>
        </a>
    </div>
</div>