jquery - 从百分比到初始的 CSS 过渡

标签 jquery html css css-transitions

我在做什么:拿这个:

#prod-img {
    width: 33%;
    max-width: 33%;
    margin-right: 3%;
    display: inline-block;
    float: left;
}

并尝试过渡到此:

#prod-img:hover, #prod-img:active{
    width: auto;
    float: none;
    display: block;
    max-width: 100%;
    height: auto;
    margin-left: auto;
    margin-right: auto;
}

Here是一个 jsFiddle,展示了它的实际效果。

问题:除非您不能使用最大宽度,否则从百分比到初始值的 CSS 过渡非常好。我需要使用 max-width 来实际限制宽度,所以我无法执行建议的操作 here .我不能使用宽度,因为所有图像都有不同的宽度。

我的尝试:使用宽度、边距和高度。我还尝试使用 jQuery .addClass().css() 但是当我将鼠标悬停在上面时绝对没有任何反应。

期望的结果:一个丝般顺畅的 CSS 过渡,从小的、向左浮动的到大的、居中的任何尺寸的图像。

这是 HTML 的样子(我使用 angular 来处理大量这些图像):

<div>
    <img id="prod-img" src="{{product.url}}" alt="{{product.name}}" />
</div>

最佳答案

由于您没有提供相应的 HTML,我假设并创建了这样的东西。

#prod-img img{    
    margin:0px;
    width:33%;
    transition:2s;
    
}
#prod-img:hover img, #prod-img:active img{    
    width:400px; /*width must be specified*/
    transform:scale(1.2); /*scale as much as you need*/
    margin-left:calc(50% - 200px); /* Calculate the half of the image width */
}
<div id="prod-img">
    <img src="/image/3NQEB.png?s=328&g=1">
</div>

希望对您有所帮助!

关于jquery - 从百分比到初始的 CSS 过渡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32582022/

相关文章:

jquery - 获取 JQuery 对话框的子窗口位置

jquery - 使用jquery根据td值添加类

javascript - 使用 jQuery 缓存模糊事件

jquery - 当通过提交按钮以外的其他方式提交表单时,如何触发事件?

javascript - HTML 5 表单输入验证

html - 旋转时如何将 span 或 div 对齐到中间?

javascript - Treant 可折叠节点文本方向

javascript - 如何使用 Javascript 从输入框值计算要支付的金额?

javascript - 在 SessionStorage 中保存 DOM 元素

html - 谷歌字体系列。按类引用特定字体。如何?