myFunc.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * 获取滚动的头部距离和左边距离
  3. * scroll().top scroll().left
  4. * @returns {*}
  5. */
  6. function scroll() {
  7. if(window.pageYOffset !== null){
  8. return {
  9. top: window.pageYOffset,
  10. left: window.pageXOffset
  11. }
  12. }else if(document.compatMode === "CSS1Compat"){ // W3C
  13. return {
  14. top: document.documentElement.scrollTop,
  15. left: document.documentElement.scrollLeft
  16. }
  17. }
  18. return {
  19. top: document.body.scrollTop,
  20. left: document.body.scrollLeft
  21. }
  22. }
  23. function $(id) {
  24. return typeof id === "string" ? document.getElementById(id) : null;
  25. }
  26. /**
  27. * 获取屏幕的宽度和高度
  28. * @returns {*}
  29. */
  30. function client() {
  31. if(window.innerWidth){ // ie9+ 最新的浏览器
  32. return {
  33. width: window.innerWidth,
  34. height: window.innerHeight
  35. }
  36. }else if(document.compatMode === "CSS1Compat"){ // W3C
  37. return {
  38. width: document.documentElement.clientWidth,
  39. height: document.documentElement.clientHeight
  40. }
  41. }
  42. return {
  43. width: document.body.clientWidth,
  44. height: document.body.clientHeight
  45. }
  46. }
  47. /**
  48. * 匀速动画函数
  49. * @param {object}obj
  50. * @param {number}target
  51. * @param {number}speed
  52. */
  53. function constant(obj, target, speed) {
  54. // 1. 清除定时器
  55. clearInterval(obj.timer);
  56. // 2. 判断方向
  57. var dir = obj.offsetLeft < target ? speed : -speed;
  58. // 3. 设置定时器
  59. obj.timer = setInterval(function () {
  60. obj.style.left = obj.offsetLeft + dir + "px";
  61. if(Math.abs(target - obj.offsetLeft) < Math.abs(dir)){
  62. clearInterval(obj.timer);
  63. obj.style.left = target + "px";
  64. console.log(obj.offsetLeft, target);
  65. }
  66. }, 20);
  67. }