当前位置:   article > 正文

vue解决页面放大图片模糊的问题

vue解决页面放大图片模糊的问题

1.页面放大(或者电脑设置了缩放比例,比如125%),图片模糊

不考虑带宽的情况下,直接请求后端最大尺寸的照片。

2.根据用户电脑的放大倍数或者电脑设置中的放大倍数(DPR),自动请求合适的照片

3.实现:

记住公式:图片尺寸 = css尺寸 * DPR,只要公式成立,就一定是清晰的

4.利用img标签的srcset属性实现,值为:图片地址 + DPR

  1. <template>
  2. <div>
  3. <img srcset="
  4. https://picsum.photos/id/88/150 1x,
  5. https://picsum.photos/id/88/300 2x,
  6. https://picsum.photos/id/88/600 4x,
  7. https://picsum.photos/id/88/900 6x,
  8. https://picsum.photos/id/88/1200 8x,
  9. " alt="">
  10. </div>
  11. </template>
  12. <script>
  13. export default{
  14. name: 'autoImg',
  15. data(){
  16. return {
  17. }
  18. }
  19. }
  20. </script>
  21. <style lang="less" scoped>
  22. img{
  23. width: 150px;
  24. height: 150px;
  25. }
  26. </style>

5.万一图片的css样式(宽高)不是写死的,用的是百分比,该如何实现?

记住公式:图片尺寸 = css尺寸 * DPR,现在css尺寸不固定,DPR也不固定

6.使用srcset+sizes属性实现,srcset的值改为:图片地址+图片原始尺寸,sizes采用媒介查询,当照片宽度最大是300px时,就当作是150px,然后乘以DPR,会自动选择合适图片地址。

  1. <template>
  2. <div>
  3. <img srcset="
  4. https://picsum.photos/id/88/150 150w,
  5. https://picsum.photos/id/88/300 300w,
  6. https://picsum.photos/id/88/600 600w,
  7. https://picsum.photos/id/88/900 900w,
  8. https://picsum.photos/id/88/1200 1200w,
  9. "
  10. sizes="
  11. (max-width: 300px) 150px, //当照片宽度最大是300px时,就当作是150px,然后乘以DPR(当为6时),自动选择900w的https://picsum.photos/id/88/900地址(150*6=900)
  12. (max-width: 600px) 300px,
  13. (max-width: 900px) 450px,
  14. (max-width: 1200px) 600px,
  15. 1200px
  16. "
  17. alt="">
  18. </div>
  19. </template>
  20. <script>
  21. export default {
  22. name: 'autoImg',
  23. data() {
  24. return {
  25. };
  26. }
  27. };
  28. </script>
  29. <style lang="less" scoped>
  30. img {
  31. width: 50vw;
  32. height: 50vh;
  33. display: block;
  34. margin: 0 auto;
  35. }
  36. </style>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号