您好!欢迎访问家园网-www.jy.wang!

家园网

看html代码,其中惠普电脑的css类为mr-u-sm-7 mr-u-md-4 text-two sug,但是最后第4个css是如何实现图片悬停后向右移动效果的?什么在起作用?

网络 作者:本站 点击:

看html代码,其中惠普电脑的css类为

mr-u-sm-7 mr-u-md-4 text-two sug,

但是最后第4个css是如何实现图片悬停后向右移动效果的?什么在起作用?


惠普电脑图片悬停向右移动效果的CSS分析

根据您的HTML结构,惠普电脑的图片在:

html

<div class="mr-u-sm-7 mr-u-md-4 text-two sug">
    <div class="outer-con ">
        <div class="title ">惠普(HP)笔记本</div>
        <div class="sub-title ">¥4999.00</div>
        <i class="mr-icon-shopping-basket mr-icon-md  seprate"></i>
    </div>
    <a href="shopList.html"><img src="images/computer1.jpg"/></a></div>


一、关键CSS代码定位

在 hmstyle.css 中,我找到了控制惠普电脑图片悬停效果的CSS:

位移效果规则(约第1611-1665行)

css

/* 位移效果 - 控制所有楼层图片的悬停移动 */.text-one img, .text-two img, .text-three img, .text-four img, .text-five img, .text-six img, .text-seven img, .recommendationMain img {
    transition: all .2s;  /* 所有图片都有0.2秒的平滑过渡 */
    border: none;}/* 具体到 .text-two 图片的悬停效果 */.floodThree .text-four:hover img, .floodThree .text-five:hover img, .floodFour .text-two:hover img,      /* ← 这条规则!*/.floodFour .text-three:hover img, .floodFive .text-two:hover img, 
.floodFive .text-three:hover img, 
.floodFive .text-five:hover img, 
.floodFive .text-six:hover img {
    margin-right: -40px;              /* 右边距减少40px */}


二、为什么是 floodFour .text-two:hover img 这条规则?

您的惠普电脑在 #f2(电脑楼层)中:

html

<div id="f2">
    <div class="floodFour" id="f2">   <!-- ← 注意!id="f2",但class="floodFour" -->
        ...        <div class="mr-u-sm-7 mr-u-md-4 text-two sug">   <!-- ← class="text-two" -->
            <img src="images/computer1.jpg"/>
        </div>
    </div></div>

关键点

  • 父容器 div 的 class 是 floodFour

  • 图片所在 div 的 class 包含 text-two

所以 floodFour .text-two:hover img 这条规则精确匹配了您的惠普电脑图片!


三、为什么图片向右移动?

规则内容

css

.floodFour .text-two:hover img {
    margin-right: -40px;   /* 右边距减少40px(负值) */}

效果分析

初始状态

  • 图片在父容器中可能是居中或正常位置

  • margin-right: 0(默认)

悬停状态

  • margin-right: -40px

  • 右边距变成负数(-40px)

负边距的效果

  • 负的 margin-right 会让元素向右延伸或移动

  • 因为右边距减少,元素会占用更多的右侧空间

  • 视觉上:图片向右侧移动了!


四、示意图

text

初始状态(margin-right: 0):
┌─────────────────────────────────────────────┐
│  ┌─────────────────────────────────────────┐│
│  │                                         ││
│  │              ┌───────┐                  ││
│  │              │ 图片  │                  ││
│  │              └───────┘                  ││
│  │              ↑                         ││
│  │          右边距为0                      ││
│  └─────────────────────────────────────────┘│
└─────────────────────────────────────────────┘

悬停状态(margin-right: -40px):
┌─────────────────────────────────────────────┐
│  ┌─────────────────────────────────────────┐│
│  │                                         ││
│  │                  ┌───────┐              ││
│  │                  │ 图片  │              ││
│  │                  └───────┘              ││
│  │                         ↑               ││
│  │                     右边距-40px         ││
│  │                     (向右多占40px)     ││
│  └─────────────────────────────────────────┘│
└─────────────────────────────────────────────┘

图片向右移动了!


五、为什么是 -40px 而不是 +50px

楼层图片类型悬停效果移动方向
一楼(floodOnetext-onemargin-right: 50px向左移动
四楼(floodFourtext-twomargin-right: -40px向右移动
三楼(floodThreetext-fourtext-fivemargin-right: -40px向右移动

为什么不同方向?

  • 正数 margin-right(如50px):增加右边距,图片被向左挤

  • 负数 margin-right(如-40px):减少右边距,图片向右延伸


六、transition: all .2s 的作用

css

.text-one img, .text-two img, ... {
    transition: all .2s;}

这条规则让 margin-right 从 0 到 -40px 的变化不是瞬间完成,而是在0.2秒内平滑地移动,产生流畅的滑动效果。


七、总结

问题答案
哪条CSS在起作用?.floodFour .text-two:hover img { margin-right: -40px; }
为什么匹配到惠普电脑?父容器 class=floodFour,图片div class=text-two
为什么向右移动?margin-right: -40px(负边距)让图片向右延伸
为什么有平滑效果?transition: all .2s 让变化在0.2秒内平滑过渡
与电脑大图效果的区别?大图用正边距(向左移),小图用负边距(向右移)

一句话总结:惠普电脑图片悬停向右移动,是因为 hmstyle.css 中的 .floodFour .text-two:hover img { margin-right: -40px; } 规则生效,负的右边距让图片向右移动,配合 transition: all .2s 产生平滑的滑动动画



标签: