css - 应用过渡和变换的不同方式?
问题描述
我分别在我的盒子 div 集合的第一个孩子和其他孩子上应用悬停变换。但是,当我将鼠标悬停在第一个孩子之外时,这会导致一个问题,因为第一个孩子和其他孩子的转换属性仍然相同,但转换不同。因此,我正在寻找其他方法来对第一个孩子应用过渡。
每个网格上第一个项目的悬停问题 - https://netflix-clone-by-shivam.herokuapp.com/
我尝试在解决问题的转换属性上使用 not(:first-child) ,但现在它没有转换。我还尝试使用 javascript 添加过渡属性,但它使过渡的问题又回来了。
//react - mapping over collectionItems is returning divs with class box
<div className="preview">
{
movies
? (movieData.map(({id, ...otherProps}) => <CollectionItem key={id} {...otherProps} />))
: null
}
{
tvshow
? (tvData.map(({id, ...otherProps}) => <CollectionItem key={id} {...otherProps} />))
: null
}
</div>
//css
.preview {
display: flex;
cursor: pointer;
&:hover {
.box {
transform: translateX(-25%); //moves items on the left by 25%
}
}
.box:hover ~ .box {
transform: translateX(25%); //moves items on the right by 25%
}
.box:first-child:hover ~ .box {
transform: translateX(50%); //moves items to the right of firstchild by 50%
}
.box {
&:not(:first-child) {
transition: transform 300ms ease 100ms;
}
&:hover {
transform: scale(1.5);
}
&:hover:first-child {
transform-origin: left;
transform: scale(1.5);
}
}
}
解决方案
You don't need to handle it separately. You simply need to set your transform-origin: left;
on :first-child
only (no hover). By placing it in :hover
it is only is applied on hover vs on hover and hover out. Try replacing your current .box
block at the end of your code with this:
.box {
transition: transform 300ms ease 100ms;
&:first-child { /* should only be &:fist-child */
transform-origin: left;
}
&:hover {
transform: scale(1.5);
}
}
推荐阅读
- python - 如何使用 python shell 本身运行新的 python shell
- java - 将 postgresql join 和 group by 查询转换为 JPA 标准 API
- lua - Moho,运行脚本并获取 ScriptInterface 实例
- python - 如何填充接触图像边框的轮廓?
- javascript - 从函数访问范围
- vue.js - linting 时我应该使用“plugin:vue/essential”和“plugin:vue/vue3-recommended”吗?
- r - 想要根据匹配的时间戳将一个数据帧中的某些行替换为另一个数据帧中的行(两个数据帧的时间戳都在相同的 tz 中)
- reactjs - onChange 没有为自定义子组件触发
- android - 如何在android中本地化地理编码器上的响应
- javascript - 如何在事件监听器上获取第一个 div 元素