【发布时间】:2021-11-23 03:09:16
【问题描述】:
我有一个带有 V 形的简单 html 元素,我想在下拉列表更改时旋转它,但是它不是自行旋转,而是在 V 形的尖端旋转,使其改变位置,我尝试使用 transform-origin 但它仍然不起作用,任何想法为什么?
html元素:
<div :class="[{ 'opened': isOpen }]">
<div
class="title"
@click="toggle()" //The toggle will just toggle the isOpen property
>
{{ title }}
</div>
</div>
css:
.title {
width: 100%;
position: relative;
margin-top: 5px;
padding-right: 1.5em;
&:after {
content: "";
background-image: url(data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 14'><path d='M1.9 13.8c-.1.1-.3.2-.5.2s-.4-.1-.5-.2l-.7-.7c-.1-.2-.2-.4-.2-.6s.1-.4.2-.5l4.7-5L.2 2c-.1-.1-.2-.3-.2-.5s.1-.4.2-.5L.9.3c.1-.2.3-.3.5-.3s.4.1.5.2l5.8 6.2c.2.2.3.4.3.6 0 .2-.1.4-.2.5l-5.9 6.3z' fill='%21262674'/></svg>);
background-repeat: no-repeat;
position: absolute;
width: 1.5em;
height: 1em;
transition: transform .3s;
margin-top: 5px;
transform: rotate(90deg);
transform-origin: center;
top: calc(50% - .5em);
right: 0;
}
}
当下拉菜单打开时:
.opened {
.title {
transition: rotate 300ms ease-in;
&:after {
content: "";
background-image: url(data:image/svg+xml;charset=utf-8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 14'><path d='M1.9 13.8c-.1.1-.3.2-.5.2s-.4-.1-.5-.2l-.7-.7c-.1-.2-.2-.4-.2-.6s.1-.4.2-.5l4.7-5L.2 2c-.1-.1-.2-.3-.2-.5s.1-.4.2-.5L.9.3c.1-.2.3-.3.5-.3s.4.1.5.2l5.8 6.2c.2.2.3.4.3.6 0 .2-.1.4-.2.5l-5.9 6.3z' fill='%21262674'/></svg>);
background-repeat: no-repeat;
position: absolute;
width: 1.5em;
height: 1em;
transition: transform .3s;
margin-top: 5px;
transform: rotate(270deg);
transform-origin: center;
top: calc(50% - .5em);
right: 0;
}
}
}
【问题讨论】:
-
请问有相应的HTML吗?它会更容易理解。
-
你确定背景图片在伪元素的中心吗?
-
我通过调整顶部和底部的位置/边距来做到这一点,具体取决于它是向上还是向下,因为我不想重写 SVG(我使用的是 svg 图像库)
标签: css svg transform rotatetransform