php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 626|回复: 0

JS实现弹性漂浮广告代码

[复制链接]

2667

主题

2674

帖子

9482

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6693
贡献
0
注册时间
2021-4-14
最后登录
2024-5-12
在线时间
673 小时
QQ
发表于 2022-1-16 15:56:10 | 显示全部楼层 |阅读模式
弹性悬浮广告效果图
image.png
1.功能(鼠标移入移出事件、点击事件、定时器控制移动)
div实现在页面上移动,并判断碰到页面边框反弹,鼠标移入div停止移动,鼠标移出div进行移动,点击div让它从页面消失,定时器控制它的移动。

2.div初始样式设置
[mw_shl_code=applescript,true]        //div(广告)获取节点
        var div1 = document.getElementById("div1");
        // div(广告)初始的位置
        var offsetx = 0;
        var offsety = 0;
        //div(广告)每次移动的距离
        var stepx = 10;
        var stepy = 10;
        //div(广告)的大小
        div1.style.width="100px"
        div1.style.height="100px"
        //设置定位
        div1.style.position="absolute"
        div1.style.top = offsetx;
        div1.style.left = offsety;
        //广告图片
        div1.style.backgroundImage="url(./img/ggao.webp)"
        div1.style.backgroundSize="cover"[/mw_shl_code]
2.获取div可以移动的页面大小
[mw_shl_code=applescript,true]        //网页可视化宽高--div(广告)可以移动的区域
        var seeWidth = document.documentElement.clientWidth;
        var seeHeight = document.documentElement.clientHeight;
        //div(广告)最大可移动的宽度、高度
        var maxLeft = seeWidth -100;
        var maxTop = seeHeight -100;[/mw_shl_code]
3.定时器控制移动,鼠标移入事件停止移动,鼠标移出继续移动。
[mw_shl_code=applescript,true]       //启动定时器
        var t= setInterval(move,30);
        //鼠标移入清除定时器
        div1.onmouseenter = function(){
            clearInterval(t);
        }
        //鼠标移出恢复
        div1.onmouseleave = function(){
            t = setInterval(move,30);
        }[/mw_shl_code]
4.点击事件点击让div消失
[mw_shl_code=applescript,true]        //点击事件,点击后消失
        div1.onclick = function(){
            div1.style.display = "none"
        } [/mw_shl_code]
5.实现悬浮广告的完整代码
[mw_shl_code=applescript,true]<div id="div1">  </div>
<body>
    <script>
        //div(广告)获取节点
        var div1 = document.getElementById("div1");
        // div(广告)初始的位置
        var offsetx = 0;
        var offsety = 0;
        //div(广告)每次移动的距离
        var stepx = 10;
        var stepy = 10;
        //div(广告)的大小
        div1.style.width="100px"
        div1.style.height="100px"
        //设置定位
        div1.style.position="absolute"
        div1.style.top = offsetx;
        div1.style.left = offsety;
        // div1.style.backgroundColor="black"
        div1.style.backgroundImage="url(./img/ggao.webp)"
        div1.style.backgroundSize="cover"
        
        //网页可视化宽高--div(广告)可以移动的区域
        var seeWidth = document.documentElement.clientWidth;
        var seeHeight = document.documentElement.clientHeight;
        //div(广告)最大可移动的宽度、高度
        var maxLeft = seeWidth -100;
        var maxTop = seeHeight -100;
        function move(){
            offsetx+=stepx;
            offsety+=stepy;
            console.log(offsetx);
            console.log(offsety)
            //大于可移动的高度或到达顶部 就让移动的距离变为它的负数
            if(offsety>=maxTop||offsety<=0){
                stepy = -stepy;
            }
            //大于可移动的宽度或到达最左 就让移动的距离变为它的负数
            if(offsetx>=maxLeft||offsetx<=0){
                stepx=-stepx;
            }
            //div定位的位置
            div1.style.top = offsety+"px"
            div1.style.left = offsetx+"px"
        }
        //启动定时器
        var t= setInterval(move,30);
        //鼠标移入清除定时器
        div1.onmouseenter = function(){
            clearInterval(t);
        }
        //鼠标移出恢复
        div1.onmouseleave = function(){
            t = setInterval(move,30);
        }
        //点击事件,点击后消失
        div1.onclick = function(){
            div1.style.display = "none"
        }
    </script>[/mw_shl_code]





上一篇:HTML5新增标签
下一篇:前后端分离 -- 深入浅出 Spring Boot + Vue + ElementUI 实现相册管...
回复

使用道具 举报

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

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )

GMT+8, 2024-5-14 20:50 , Processed in 0.154293 second(s), 35 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表