通过鼠标使图片交替显示

伪类 after、before、hover

通过鼠标使图片交替显示
css代码使用的是预编译less,
简单分析:就是固定框200*200,设定超出不显示,在div同时放入两张图片,通过margin的方式进行切换,效果由transition属性,显得较为平滑

.all_size(@width:200px,@height:200px) {
  width: @width;
  height: @height;
}

.img_style(@url) {
  .all_size();
  content: "";
  display: inline-block;
  background-size: contain;
  background: url(@url) no-repeat center;
}
.img {
  .all_size();
  display: inline-block;
  overflow: hidden;

  .img_list {
    .all_size(400px,200px);
    display: inline-block;

    &::before {
      .img_style("../assets/imgaes/1.jpg");
      transition: margin 0.2s;
    }
    &::after {
      .img_style("../assets/imgaes/2.jpg");
    }
    &:hover::before {
      margin-left: -400px;
      transition: margin 0.2s;
    }
  }
}
<div class="img">
   <div class="img_list"></div>
</div>

如果看不懂less,就。。。

.img {
  display: inline-block;
  width: 200px;
  height: 200px;
  overflow: hidden;
}

.img_list {
  display: inline-block;
  width: 400px;
  height: 200px;
}

.img_list::before {
  content: "";
  display: inline-block;
  width: 200px;
  height: 200px;
  background: url("../assets/imgaes/1.jpg") no-repeat center;
  background-size: contain;
  transition: margin 0.2s;
}

.img_list::after {
  content: "";
  display: inline-block;
  width: 200px;
  height: 200px;
  background: url("../assets/imgaes/2.jpg") no-repeat center;
  background-size: contain;
}

.img_list:hover::before {
  margin-left: -400px;
  transition: margin 0.2s;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值