当前位置:Gxlcms > css > css平行四边形与菱形变换

css平行四边形与菱形变换

时间:2021-07-01 10:21:17 帮助过:13人阅读

*以下技巧均源自于Lea Verou所著《CSS Secrets》

平行四边形

  平行四边形的构造可以基于矩形通过skew()的变形属性进行斜向拉升得到(skew所用的坐标系,纵向是X轴,横向是Y轴,与常见的坐标系相反)。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .location{
  8. position: relative;
  9. top: 150px;
  10. left: 150px;
  11. }
  12. .button{
  13. color: white;
  14. background-color: #51bfff;
  15. width: 120px;
  16. height: 50px;
  17. line-height: 50px;
  18. text-align: center;
  19. transform: skewX(-45deg);
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="location button">click</div>
  25. </body>
  26. </html>

weirui1590_1480483470530_59.png

 但是内容倾斜可能不是我们所需要的效果,一种常规的解决方案就是在内层嵌套一个div,然后加上一个反方向的拉升transform: skewX(45deg);但是有代码洁癖的人表示接受不能。

  另一种思路是将所有样式应用到伪元素上。然后再对伪元素进行变形。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .location{
  8. position: relative;
  9. top: 150px;
  10. left: 150px;
  11. }
  12. .button{
  13. width: 120px;
  14. height: 50px;
  15. line-height: 50px;
  16. text-align: center;
  17. color: white;
  18. }
  19. .button:before{
  20. content: '';
  21. position: absolute;
  22. top: 0; right: 0; bottom: 0; left: 0;
  23. background-color: #51bfff;
  24. transform: skewX(-45deg);
  25. z-index: -1;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div class="location button">click</div>
  31. </body>
  32. </html>

weirui1590_1480483470530_59.png

  这样不但解决了内容倾斜的问题,而且html结构还是和之前一样干净。不过要注意伪元素生成的图案是重叠在内容之上的,一旦给它设置背景,就会遮住内容,所以要加上z-index: -1。

菱形图片

  如果是在正方形的基础之上,菱形就是正方形图案旋转45度的图形。我们很容易想到将外层div旋转45度再将内层img反向旋转45度。得到如下的图案。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .location{
  8. position: relative;
  9. top: 150px;
  10. left: 150px;
  11. }
  12. .picture{
  13. width: 600px;
  14. transform: rotate(45deg);
  15. overflow: hidden;
  16. }
  17. .picture>img{
  18. max-width: 100%;
  19. transform: rotate(-45deg);
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="location picture"><img src="1.jpeg"></div>
  25. </body>
  26. </html>

weirui1590_1480483470530_59.png

  是个挺漂亮的正八边形呢,如果你能说服产品经理,那工作也算完成啦。好吧,我猜你不能说服。。。

  这里由于旋转方向不一致,外层div截取了超出的部分(注意overflow:hidden),然后一部分又空出来了。只要填充完空出的部分就是菱形啦,这里你可以画个草图用勾股定理算一算。

  算出的结果是放大1.42倍填充完全。我们在img的transform属性改为transform: rotate(45deg) scale(1.42);得到下图:

weirui1590_1480483470530_59.png

 这个构造菱形的方案有一个缺陷,就是当原图不是正方形就需要使用更大的放大系数,截取的图片内容就更有限了。

  而且方案本身也不算简洁优雅。这里向大家介绍一个属性clip-path(遗憾的是支持性似乎并不好),它可以通过传入固定的位置点将图片裁剪成任意的多边形。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .location{
  8. position: relative;
  9. top: 150px;
  10. left: 150px;
  11. }
  12. .picture{
  13. -webkit-clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
  14. -moz-clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
  15. clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
  16. transition: 1s clip-path;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <img class="location picture" src="1.jpeg">
  22. </body>
  23. </html>

weirui1590_1480483470530_59.png

 希望在不远的将来clip-path这个属性在各游览器得到更好的支持。

人气教程排行