index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //DOM操作、Ajax
  2. import 'http://code.jquery.com/jquery-2.1.1.min.js';
  3. //轮播图
  4. import Swiper from 'https://cdn.staticfile.org/Swiper/8.2.2/swiper-bundle.esm.browser.min.js';
  5. window.Swiper = Swiper;
  6. // bootstrap
  7. import 'https://cdn.staticfile.org/bootstrap/5.1.3/js/bootstrap.min.js';
  8. /**
  9. * 执行动画
  10. * @description 如果元素滚动至可视窗口,添加css动效
  11. * @param ele 元素的选择器
  12. */
  13. window.showAnimate = function (ele, className) {
  14. //先隐藏元素
  15. $(ele).css({
  16. 'visibility': 'hidden'
  17. })
  18. /**
  19. *
  20. * @description 得到元素的坐标信息
  21. */
  22. function GetRect (element) {
  23. let rect = element.getBoundingClientRect() // 距离视窗的距离
  24. let top = document.documentElement.clientTop ? document.documentElement.clientTop : 0 // html元素对象的上边框的宽度
  25. let left = document.documentElement.clientLeft ? document.documentElement.clientLeft : 0
  26. return {
  27. top: rect.top - top,
  28. bottom: rect.bottom - top,
  29. left: rect.left - left,
  30. right: rect.right - left
  31. }
  32. }
  33. let $box = $(ele)[0]
  34. let divH = $box.offsetHeight // div高度
  35. let windowH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight // 浏览器高度兼容写法
  36. window.onscroll = function () {
  37. let obj = GetRect($box)
  38. if (obj.top >= windowH) { // 在视口之下
  39. }
  40. if (obj.top < windowH && obj.bottom >= windowH) { // 正在出现
  41. }
  42. if (obj.top > 0 && obj.top < windowH && obj.bottom > 0 && obj.bottom < windowH) { // 正在视口中
  43. //添加动效
  44. if(!$(ele).hasClass(className)){
  45. $(ele).css({
  46. 'visibility': 'unset'
  47. })
  48. $(ele).addClass(className);
  49. }
  50. }
  51. if (obj.top <= 0 && obj.bottom <= divH && obj.bottom > 0) { // 正在离开视口
  52. }
  53. if (obj.bottom <= 0) { // 已经离开视口
  54. }
  55. }
  56. }
  57. /**
  58. * @description 元素横向滚动至父容器的中间
  59. * @param ele 可以传元素的"event"或者"选择器名称"
  60. * @fatherEle 可滚动的父容器的元素
  61. */
  62. window.eleToCenter = function (e,fatherEle){
  63. let item = typeof e == 'object'?$(e.target):(e);
  64. let container = $(fatherEle);
  65. let itemOffset = item.offset();
  66. let itemOffsetLeft = itemOffset.left + container.scrollLeft();
  67. let centerLeft = ( container.width() - item.width()) / 2 + item.width()*0.5;
  68. container.animate({
  69. scrollLeft: itemOffsetLeft - centerLeft
  70. },300)
  71. }