resize.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * Created by zhengguorong on 2016/11/9.
  3. */
  4. //设计图宽高比
  5. var designwhScale = 320/508;
  6. //现窗口宽高比
  7. var curwhScale = window.innerWidth/window.innerHeight;
  8. var resizes = document.querySelectorAll('.resize');
  9. //外层容器定位
  10. var swiperContainer = document.querySelector('.container');
  11. var containerWidth = 320;
  12. var containerHeight = 508;
  13. var containerTop = 0;
  14. var containerLeft = 0;
  15. if (curwhScale < designwhScale) {
  16. containerWidth = window.innerWidth
  17. containerHeight = window.innerWidth / designwhScale
  18. containerTop = (window.innerHeight - containerHeight)/2
  19. }else {
  20. containerWidth = window.innerHeight * designwhScale;
  21. containerHeight = window.innerHeight;
  22. containerLeft = (window.innerWidth - containerWidth)/2
  23. }
  24. swiperContainer.style.width = containerWidth;
  25. swiperContainer.style.height = containerHeight;
  26. swiperContainer.style.left = containerLeft;
  27. swiperContainer.style.top = containerTop;
  28. //放大比例
  29. var scale = containerWidth / 320
  30. //元素缩放
  31. for (var j = 0; j < resizes.length; j++) {
  32. resizes[j].style.width = parseInt(resizes[j].style.width) * scale + 'px';
  33. resizes[j].style.height = parseInt(resizes[j].style.height) * scale + 'px';
  34. resizes[j].style.left = parseInt(resizes[j].style.left) * scale + 'px';
  35. resizes[j].style.top = parseInt(resizes[j].style.top) * scale + 'px';
  36. resizes[j].style.right = parseInt(resizes[j].style.right) * scale + 'px';
  37. }