纯CSS实现图像鼠标悬停效果

分手后的思念是犯贱 2022-08-26 11:19 398阅读 0赞

前端开发whqet,csdn,王海庆,whqet,前端开发专家

今天来看一组纯CSS实现的鼠标悬停效果,在线研究代码点效果一、效果二、效果三,下载收藏点这里,效果预览点这里。

效果1

效果1的html文件

  1. <div class="pic">
  2. <img src="pic.jpg" alt="">
  3. <span>这里是个标题呀</span>
  4. </div>

css文件中我们主要实现定位和hover动画。

  1. /* 图片盒子的样式,宽高、边框、阴影、位置、鼠标样式、溢出 */
  2. .pic{
  3. width: 500px;
  4. height: 500px;
  5. border: 10px solid #333;
  6. box-shadow: 0 0 10px rgba(0,0,0,.6);
  7. margin: 50px auto;
  8. position: relative;
  9. overflow: hidden;
  10. cursor: pointer;
  11. }
  12. /* 图片标题的样式,宽高、背景色、水平垂直居中、定位,透明度 */
  13. .pic span{
  14. position: absolute;
  15. left: 0;
  16. bottom: 0;
  17. width: 500px;
  18. height: 50px;
  19. background: rgba(0,0,0,.5);
  20. color: #fff;
  21. font-size: 24px;
  22. line-height: 50px;
  23. text-align: center;
  24. opacity: 0;
  25. }
  26. /* 给需要动画的元素加过渡属性 */
  27. .pic img,.pic span{
  28. transition: all .5s;
  29. }
  30. /* hover之后透明动画 */
  31. .pic:hover span{
  32. opacity: 1;
  33. }
  34. /* hover之后图片放大的效果 */
  35. .pic:hover img{
  36. transform: scale(2);
  37. }

效果2

下面看效果2,上下滑动打开的实现过程。首先看html文件,我们需要两个图片元素,一个显示上半部分,一个显示下半部分。

  1. <div class="pic">
  2. <img class="top" src="pic.jpg" alt="top">
  3. <img class="bottom" src="pic.jpg" alt="bottom">
  4. <span>OK,You can see it.</span>
  5. </div>

css文件如下,我们把实现原理写到注释里,就不一一解释了。

  1. /* 图像盒子的样式设置,宽高、边框、阴影、溢出、位置 */
  2. div.pic{
  3. width: 500px;
  4. height: 500px;
  5. cursor: pointer;
  6. margin: 50px auto;
  7. position: relative;
  8. overflow: hidden;
  9. border: 10px solid #333;
  10. box-shadow: 0 0 10px rgba(0,0,0,.8);
  11. }
  12. /* 上下两半部分的统一设置,绝对定位和过渡 */
  13. div.pic img{
  14. position: absolute;
  15. left: 0;
  16. transition: all 1s;
  17. }
  18. /* 上半部分图像的裁剪和定位 */
  19. div.pic img.top{
  20. top: 0;
  21. clip: rect(0px,500px,250px,0px);
  22. }
  23. /* 下半部分图像的裁剪和定位 */
  24. div.pic img.bottom{
  25. bottom: 0;
  26. clip: rect(250px,500px,500px,0px);
  27. }
  28. /* hover之后图像的定位改变 */
  29. div.pic:hover img.top{
  30. top: -50px;
  31. }
  32. div.pic:hover img.bottom{
  33. bottom: -50px;
  34. }
  35. /* 图像标题的样式设置,文字颜色、水平居中、垂直居中 */
  36. div.pic span{
  37. display: inline-block;;
  38. width: 100%;
  39. text-align: center;
  40. line-height: 500px;
  41. font-size: 24px;
  42. }

重点解释下CSS Clip的裁剪原理。

  1. clip: rect(<top>, <right>, <bottom>, <left>);

这里的top、right、bottom、left指的是裁剪之后的矩形的上右下左四个边到原始边的距离,如下图所示

SouthEast
大家可以看看张鑫旭大牛的《CSS clip:rect矩形剪裁功能及一些应用介绍》了解详情,或者通过在线体会案例体验裁剪效果。

大家也可以利用Codrops上的《Expanding Overlay Effect》学习clip属性的超酷应用。

效果3

效果3的难点在于三角线图像的实现,我们这里主要利用skew()实现。html文件是这样的,我们需要分别在两个图像上加容器,然后分别对图像容器和图像设置相反方向的斜切。

  1. <div class="pic">
  2. <div class="top">
  3. <img src="http://gx.zptc.cn/whqet/img/road.jpg">
  4. </div>
  5. <div class="bottom">
  6. <img class="bottom" src="http://gx.zptc.cn/whqet/img/road.jpg">
  7. </div>
  8. <span>OK,You can see it.</span>
  9. </div>

效果css文件

  1. /* 图像盒子的样式,与前两个效果无异 */
  2. .pic {
  3. width: 500px;
  4. height: 500px;
  5. overflow: hidden;
  6. cursor: pointer;
  7. border: 10px solid #333;
  8. box-shadow: 0 0 10px rgba(0, 0, 0, .8);
  9. margin: 50px auto;
  10. position: relative;
  11. }
  12. /* 两个图像的小容器的定位、斜切、溢出,因为图像宽高相同,所以斜切45度 */
  13. .pic .top, .pic .bottom {
  14. width: 500px;
  15. height: 500px;
  16. overflow: hidden;
  17. position: absolute;
  18. transform: skew(-45deg);
  19. }
  20. /* 上下两个图像小容器分别设置不同的位置,变换原点 */
  21. .pic .top {
  22. left: 0;
  23. top: 0;
  24. transform-origin: 100% 0;
  25. transition:all 1s, transform 0s;
  26. }
  27. .pic .bottom {
  28. right: 0;
  29. bottom: 0;
  30. transform-origin: 0 100%;
  31. transition:all 1s, transform 0s;
  32. }
  33. /* 上下两个图像向相反方向斜切 */
  34. .pic .top img, .pic .bottom img {
  35. width: 100%;
  36. height: 100%;
  37. transform-origin: inherit;
  38. transform: skew(45deg);
  39. }
  40. /* hover之后图像小容器的变化 */
  41. .pic:hover .top {
  42. left: -20px;
  43. top: -20px;
  44. }
  45. .pic:hover .bottom {
  46. right: -20px;
  47. bottom: -20px;
  48. }
  49. /* 图像标题的设置 */
  50. .pic span {
  51. transform: rotate(-45deg);
  52. transform-origin: 0 100%;
  53. position: absolute;
  54. font-size:20px;
  55. bottom:-.5em;
  56. opacity: 0;
  57. transition: all 1s ease-in .2s;
  58. }
  59. .pic:hover span {
  60. opacity: 1;
  61. text-indent: 15em;
  62. }

实现原理如下图所示,蓝色线框代表图像盒子.top,我们分别给图像和图像盒子不同方向相同角度的斜切实现效果,只有线框内的图像显示。

SouthEast 1

这样我们可以实现图像的三角形裁切效果,根据这样的原理大家可以尝试不同形状的实现。欢迎大家提供不同的实现思路。

That’s all. 迎接鼓励,欢迎拍砖。

-———————————————————————————————

前端开发whqet,关注web前端开发技术,分享网页相关资源。
-———————————————————————————————

发表评论

表情:
评论列表 (有 0 条评论,398人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CSS鼠标悬停

    > 在学习中遇到许多好看的样式,虽然只是用HTML+CSS简单的实现,仅作为我的学习笔记和同爱好学习者的分享: 先看效果 ![这里写图片描述][70] HTM