(十三)原生js案例之瑟瑟发抖的弹窗广告

网页里面的都懂弹窗广告,经常出现下方两个角落的效果

效果实现

请添加图片描述

代码实现

  • 必要的css

  • 静态页面

<img src="../day04/images/3.png" alt="" srcset="">
img {
      width: 300px;
      display: block;
      margin: 0 auto;
      position: absolute;
      left: 300px;
    }
  • 核心代码
window.onload = function () {
      const oImg = document.querySelector('img');

      oImg.onclick = function () {
        let arr = []
        for (let i = 20; i >= 0; i -= 2) {
          arr.push(i, -i)
        }
        arr.push(0)
        const _that = this
        let index = 0
        clearInterval(oImg.timer)
        oImg.timer = setInterval(function () {
          _that.style.left = _that.offsetLeft + arr[index] + 'px';
          index++
          if (index === arr.length) {
            clearInterval(oImg.timer)
            index = 0
            oImg.timer = setInterval(function () {
              _that.style.top = _that.offsetTop + arr[index] + 'px';
              index++
              if (index === arr.length) {
                clearInterval(oImg.timer)
              }
            }, 30)
          }
        }, 30)
      }
    }

相关推荐

  1. js原生

    2024-07-20 22:38:03       27 阅读
  2. js中windowOPen方法,特征

    2024-07-20 22:38:03       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 22:38:03       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 22:38:03       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 22:38:03       45 阅读
  4. Python语言-面向对象

    2024-07-20 22:38:03       55 阅读

热门阅读

  1. NVM配置

    NVM配置

    2024-07-20 22:38:03      16 阅读
  2. 获取磁盘剩余容量-----c++

    2024-07-20 22:38:03       15 阅读
  3. springboot3.2 RedisCacheManager配置

    2024-07-20 22:38:03       16 阅读
  4. springSecurity学习之springSecurity简介

    2024-07-20 22:38:03       20 阅读
  5. 分布式锁-redisson锁重试和WatchDog机制

    2024-07-20 22:38:03       14 阅读
  6. Photoshop图层类型

    2024-07-20 22:38:03       14 阅读
  7. (一)js前端开发中设计模式前篇之对象

    2024-07-20 22:38:03       16 阅读
  8. 网络安全-网络安全及其防护措施6

    2024-07-20 22:38:03       15 阅读
  9. [C++ 入门基础 - 命名空间]

    2024-07-20 22:38:03       14 阅读