利用纯JS开发浏览器小窗口移动广告小功能

打印 上一主题 下一主题

主题 1553|帖子 1553|积分 4661

效果展示


直接上代码

假如要用到vue项目里面,直接按照vue的写法改动就行,一样平常没有多大的题目,顶部的占位是我项目需求,你可以按照要求改动。
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <script type="text/javascript" src="../js/jquery.min.js"></script>
  8. <style>
  9. * {
  10.   margin: 0;
  11.   box-sizing: border-box;
  12. }
  13. .zhanwei {
  14.   width: 100%;
  15.   height: 160px;
  16.   border: 1px solid red;
  17. }
  18. #thediv {
  19.   width: 137px;
  20.   height: 177px;
  21.   display: flex;
  22.   flex-wrap: wrap;
  23.   justify-content: center;
  24.   background-color: white;
  25.   overflow: hidden;
  26.   border-radius: 10px;
  27.   border: 1px solid red;
  28.   position: absolute;
  29.   top: 0px;
  30.   left: 0px;
  31.   z-index: 1000;
  32. }
  33. #thediv img {
  34.   width: 100%;
  35.   height: 100%;
  36.   object-fit: cover;
  37. }
  38. #thediv:hover {
  39.   cursor: pointer;
  40. }
  41. .closeButton {
  42.   width: 25px;
  43.   height: 25px;
  44.   font-size: 16px;
  45.   background-color: rgb(55 55 55 / 27%);
  46.   color: #ffffff;
  47.   text-align: center;
  48.   line-height: 25px;
  49.   border-radius: 0px 0px 0px 4px;
  50.   position: absolute;
  51.   top: 0px;
  52.   right: 0px;
  53.   opacity: 0;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <div class="zhanwei">顶部占位ie</div>
  59. <div id="thediv" ref="thediv">
  60.   <img src="../images/leader/bg.jpg" alt="" />
  61.   <div class="closeButton">×</div>
  62. </div>
  63. </body>
  64. <script>
  65. // $(document).ready(function () {
  66.   let xPosition =0
  67.   let yPosition = 0
  68.   let step = 1
  69.   let divOffsetH = 0
  70.   let divOffsetW = 0
  71.   let yon = 0
  72.   let xia = 0
  73.   let setIntervalData = null
  74.   let thediv = document.getElementById('thediv')
  75.   let topNavH = document.getElementsByClassName('zhanwei')[0].offsetHeight
  76.   //浏览器窗口的宽度一定要打印出来看看拿到的数值是否正确
  77.   let width = document.documentElement.clientWidth || document.body.clientWidth;
  78.   //浏览器窗口的高度一定要打印出来看看拿到的数值是否正确
  79.   let height = (document.documentElement.clientHeight) - topNavH;
  80.   // 监听窗口大小变化事件,重新设置大小
  81.   window.addEventListener('resize', () => {
  82.    width = document.documentElement.clientWidth || document.body.clientWidth;
  83.    topNavH = document.getElementsByClassName('zhanwei')[0].offsetHeight
  84.    height = (document.documentElement.clientHeight) - topNavH;
  85.   });
  86.   function changePos() {
  87.    // 获取当前对象的高度
  88.    divOffsetH = thediv.offsetHeight;
  89.    // 获取当前对象的宽度
  90.    divOffsetW = thediv.offsetWidth;
  91.    //document.documentElement.scrollLeft和document.documentElement.scrollTop是浏览器滚动条移动的距离一定要打印出来看看拿到的数值是否正确
  92.    thediv.style.left = (xPosition + document.documentElement.scrollLeft) + "px";
  93.    thediv.style.top = topNavH + (yPosition + document.documentElement.scrollTop) + "px";
  94.    if (yon) {
  95.     yPosition = yPosition + step;
  96.    } else {
  97.     yPosition = yPosition - step;
  98.    }
  99.    // 当移动到浏览器边时,重设定位
  100.    if (yPosition < 0) {
  101.     yon = 1;
  102.     yPosition = 0;
  103.    }
  104.    if (yPosition >= (height - divOffsetH)) {
  105.     yon = 0;
  106.     yPosition = (height - divOffsetH);
  107.    }
  108.    if (xia) {
  109.     xPosition = xPosition + step;
  110.    } else {
  111.     xPosition = xPosition - step;
  112.    }
  113.    // 当移动到浏览器边时,重设定位
  114.    if (xPosition < 0) {
  115.     xia = 1;
  116.     xPosition = 0;
  117.    }
  118.    if (xPosition >= (width - divOffsetW)) {
  119.     xia = 0;
  120.     xPosition = (width - divOffsetW);
  121.    }
  122.   }
  123.   // 监听鼠标移入事件
  124.   thediv.addEventListener('mouseenter', clearFdAd);
  125.   // 监听鼠标移除事件
  126.   thediv.addEventListener('mouseleave', starFdAd);
  127.   // 监听鼠标点击事件
  128.   document.getElementsByClassName('closeButton')[0].addEventListener('click', function () {
  129.    close()
  130.   });
  131.   //关闭
  132.   function close() {
  133.    thediv.style.display = 'none'
  134.    setTimeout(() => {
  135.     if (setIntervalData != null) {
  136.      clearInterval(setIntervalData)
  137.      setIntervalData = null;
  138.     }
  139.    }, 800)
  140.   }
  141.   //清楚定时器
  142.   function clearFdAd() {
  143.    if (setIntervalData != null) {
  144.     clearInterval(setIntervalData)
  145.     setIntervalData = null;
  146.    }
  147.    document.getElementsByClassName('closeButton')[0].style.opacity = "1";
  148.   }
  149.   //启动定时器
  150.   function starFdAd() {
  151.    if (setIntervalData == null) {
  152.     setIntervalData = setInterval(changePos, 10)
  153.    }
  154.    document.getElementsByClassName('closeButton')[0].style.opacity = "0";
  155.   }
  156.   setTimeout(() => {
  157.    starFdAd()
  158.   })
  159. // })
  160. </script>
  161. </html>
复制代码






免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

南七星之家

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表