Sample code for implementing follow ads with JavaScript

Sample code for implementing follow ads with JavaScript

Floating ads are a very common form of advertising on websites. Floating ads can follow the user's browsing in real time, effectively convey the meaning of the product, and achieve good communication effects. So how are floating ads implemented? In fact, it is not difficult to implement floating ads. The details are as follows:

        * {
            margin: 0;
            padding: 0;
        }
        
        img {
            position: absolute;
            left: 0;
        }
        
        p {
            text-align: center;
            line-height: 40px;
        }
    <img src="images/left_ad.png" alt="">
    <p>I am the main text 1</p>
    <p>I am the main text 2</p>
    <p>I am the main text 3</p>
    <p>I am the main text 4</p>
    <p>I am the main text 5</p>
    <p>I am the main text 6</p>
    <p>I am the main text 7</p>
    <p>I am the main text 8</p>
    <p>I am the main text 9</p>
    <p>I am the main text 10</p>
    <p>I am the main text 11</p>
    <p>I am the main text 12</p>
    <p>I am the main text 13</p>
    <p>I am the main text 14</p>
    <p>I am the main text 15</p>
    <p>I am the main text 16</p>
    <p>I am the main text 17</p>
    <p>I am the main text 18</p>
    <p>I am the main text 19</p>
    <p>I am the main text 20</p>
    <p>I am the main text 21</p>
    <p>I am the main text 22</p>
    <p>I am the main text 23</p>
    <p>I am the main text 24</p>
    <p>I am the main text 25</p>
    <p>I am the main text 26</p>
    <p>I am the main text 27</p>
    <p>I am the main text 28</p>
    <p>I am the main text 29</p>
    <p>I am the main text 30</p>
    <p>I am text 31</p>
    <p>I am text 32</p>
    <p>I am the text 33</p>
    <p>I am the main text 34</p>
    <p>I am the main text 35</p>
    <p>I am text 36</p>
    <p>I am text 37</p>
    <p>I am text 38</p>
    <p>I am text 39</p>
    <p>I am Text 40</p>
    <p>I am text 41</p>
    <p>I am text 42</p>
    <p>I am text 43</p>
    <p>I am text 44</p>
    <p>I am the main text 45</p>
    <p>I am text 46</p>
    <p>I am Text 47</p>
    <p>I am Text 48</p>
    <p>I am text 49</p>
    <p>I am text 50</p>
        //1. Get the element to be operated const oAdImg = document.querySelector("img");
 
        //2. Calculate the top value of the ad image = (viewport height - ad height)/2
        const screenHeight = getScreen().height;
        const imgHeight = oAdImg.offsetHeight;
        const offsetY = (screenHeight - imgHeight) / 2;
        // console.log(offsetY);
 
        //3. Set the calculated top value to the ad image // oAdImg.style.top = offsetY + 'px';
        easeAnimation(oAdImg, {
            "top": offsetY
        });
 
        //4. Listen for scrolling events on the web page window.onscroll = function() {
            //Get the scrolling distance of the webpage //The value of the top of the ad image + the scrolling distance of the webpage let pageOffsetY = getPageScroll().y;
            easeAnimation(oAdImg, {
                "top": offsetY + pageOffsetY
            });
        };
 
        // Browser viewport width and height function getScreen() {
            let width, height;
            if (window.innerWidth) {
                width = window.innerWidth;
                height = window.innerHeight;
            } else if (document.compatMode === "BackCompat") {
                width = document.body.clientWidth;
                height = document.body.clientHeight;
            } else {
                width = document.documentElement.clientWidth;
                height = document.documentElement.clientHeight;
            }
            return {
                width: width,
                height: height
            }
        }
 
        // Ease animation function easeAnimation(ele, obj, fn) {
            clearInterval(ele.timerId);
            ele.timerId = setInterval(function() {
                //The flag variable is used to mark whether all properties have completed the animation. let flag = true;
 
                for (let key in obj) {
                    let target = obj[key];
 
                    // 1. Get the current position of the element let style = getComputedStyle(ele);
                    let begin = parseInt(style[key]) || 0;
 
                    // 2. Define variable record step size // Formula: (end position - start position) * easing coefficient (0 ~ 1)
                    let step = (target - begin) * 0.3;
 
                    // 3. Calculate the new position begin += step;
                    if (Math.abs(Math.floor(step)) > 1) {
                        flag = false;
                    } else {
                        begin = target;
                    }
                    // 4. Reset the element's position ele.style[key] = begin + "px";
                }
 
                //Judge whether the animation has been executed if (flag) {
                    //After the animation is finished, turn off the timer clearInterval(ele.timerId);
 
                    //Judge whether the fn function is passed in, and execute it if it is, otherwise do not execute fn && fn();
                }
            }, 100);
        }
 
        //Webpage scrolling distance function getPageScroll() {
            let x, y;
            if (window.pageXOffset) {
                x = window.pageXOffset;
                y = window.pageYOffset;
            } else if (document.compatMode === "BackCompat") {
                x = document.body.scrollLeft;
                y = document.body.scrollTop;
            } else {
                x = document.documentElement.scrollLeft;
                y = document.documentElement.scrollTop;
            }
            return {
                x: x,
                y: y
            }
        }

Rendering

This concludes this article about sample code for implementing follow-up ads with JavaScript. For more relevant JavaScript follow-up ads content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JavaScript implements the following scrolling buffer motion advertising frame
  • js to achieve the following mouse movement and close the picture advertising example
  • js writes the mask layer login box and couplet advertisement and automatically follows the scroll bar to scroll

<<:  Detailed explanation of Nginx process management and reloading principles

>>:  MySQL join buffer principle

Recommend

Which one should I choose between MySQL unique index and normal index?

Imagine a scenario where, when designing a user t...

Implementation of Docker deployment of web projects

The previous article has installed the docker ser...

Robots.txt detailed introduction

Basic introduction to robots.txt Robots.txt is a p...

Install Linux using VMware virtual machine (CentOS7 image)

1. VMware download and install Link: https://www....

MySQL cursor detailed introduction

Table of contents 1. What is a cursor? 2. How to ...

MySQL exposes Riddle vulnerability that can cause username and password leakage

The Riddle vulnerability targeting MySQL versions...

Detailed installation and use of RocketMQ in Docker

To search for RocketMQ images, you can search on ...

Vue implements time countdown function

This article example shares the specific code of ...

In-depth explanation of MySQL user account management and permission management

Preface The MySQL permission table is loaded into...

Detailed explanation of the general steps for SQL statement optimization

Preface This article mainly shares with you the g...

Install and build a server environment of PHP+Apache+MySQL on CentOS

Yum (full name Yellow dog Updater, Modified) is a...

Complete steps to use mock.js in Vue project

Using mock.js in Vue project Development tool sel...

HTML tag marquee realizes various scrolling effects (without JS control)

The automatic scrolling effect of the page can be...