黑帽联盟

 找回密码
 会员注册
查看: 1254|回复: 0
打印 上一主题 下一主题

[其它] JS实现悬浮移动窗口(悬浮广告)的特效

[复制链接]

148

主题

9

听众

337

积分

版主

Rank: 7Rank: 7Rank: 7

  • TA的每日心情
    擦汗
    2018-6-6 11:33
  • 签到天数: 348 天

    [LV.8]以坛为家I

    页面加载完成之后向页面插入窗口,之后向窗口插入关闭按钮,使用setInterval()函数触发moves()函数开始动画

    js方法:
    1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    2. <html>
    3.     <head>
    4.         <title> New Document </title>
    5.         <meta name="Generator" content="EditPlus">
    6.         <meta name="Author" content="">
    7.         <meta name="Keywords" content="">
    8.         <meta name="Description" content="">
    9.         <script type="text/javascript">
    10.             window.onload=function(){
    11.                 //写入
    12.                 var oneInner = document.createElement("div");
    13.                 oneInner.setAttribute("style","background:#663398;position:absolute;width:100px;height:100px;border:solid 3px #2F74A7;cursor:pointer;");
    14.                 var oneButton = document.createElement("input");
    15.                 oneButton.setAttribute("type","button");
    16.                 oneButton.setAttribute("value","x");
    17.                 if (oneButton.style.cssFloat)
    18.                 {
    19.                     oneButton.style.cssFloat="right"
    20.                 }
    21.                 else
    22.                 {oneButton.style.styleFloat="right"}
    23.                 oneInner.appendChild(oneButton);
    24.                 document.body.appendChild(oneInner);

    25.                 //定时器
    26.                 var a1a = setInterval(moves,100);
    27.                 //函数
    28.                 var x = 10;
    29.                 var y = 10;
    30.                 function moves(){
    31.                     var tops = oneInner.offsetTop
    32.                     var lefts = oneInner.offsetLeft
    33.                     if (lefts>=document.documentElement.clientWidth-oneInner.offsetWidth||lefts<=0)
    34.                     {
    35.                         x=-x
    36.                     }
    37.                     if (tops>=document.documentElement.clientHeight-oneInner.offsetHeight||tops<=0)
    38.                     {
    39.                         y=-y
    40.                     }

    41.                     tops+=y;
    42.                     lefts+=x;
    43.                     oneInner.style.top=tops+"px"
    44.                     oneInner.style.left=lefts+"px"
    45.                 }
    46.                 //悬停停止
    47.                 oneInner.onmouseover=function(){
    48.                     clearInterval(a1a);
    49.                 }
    50.                 //放手继续运动
    51.                 oneInner.onmouseout=function(){
    52.                     a1a =setInterval(moves,100);
    53.                 }
    54.                 //删除
    55.                 oneButton.onclick=function(){
    56.                     document.body.removeChild(oneInner);
    57.                 }
    58.             }
    59.         </script>
    60.     </head>
    61.     <body>

    62.     </body>
    63. </html>
    复制代码
    jquery方法:
    1. <!DOCTYPE html>
    2. <html>
    3.     <head>
    4.         <title></title>
    5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    6.         <script type="text/javascript" src="https://bbs.cnblackhat.com/js/jquery-1.7.min.js"></script>
    7.         <script>
    8.             $(function(){
    9.                 //写入div
    10.                 $("<div/>",{id:"moveWindow"}).css({width:"200px",height:"200px",border:"solid 3px #2F74A7",background:"#663398",position:"absolute",cursor:"pointer"}).appendTo("body");
    11.                 //写入关闭按钮
    12.                 $("<div/>",{id:"removeMW"}).css({width:"20px",height:"20px",background:"red",float:"right"}).appendTo("#moveWindow");
    13.                 //定时器
    14.                 var move = setInterval(moves,100);
    15.                 var x= 10;
    16.                 var y= 10;

    17.                 function moves (){
    18.                     var mw = $("#moveWindow").offset();
    19.                     var lefts =mw.left;
    20.                     var tops = mw.top;
    21.                     if (lefts>=$(window).width()-$("#moveWindow").width()||lefts<=0)
    22.                     {
    23.                         x=-x
    24.                     }
    25.                     if (tops>=$(window).height()-$("#moveWindow").height()||tops<=0)
    26.                     {
    27.                         y=-y
    28.                     }
    29.                     lefts+=x;
    30.                     tops+=y;

    31.                     $("#moveWindow").offset({top:tops,left:lefts});

    32.                 }
    33.                 //悬停停止运动
    34.                 $("#moveWindow").mouseover(function(){
    35.                     clearInterval(move);
    36.                 });
    37.                 //移开鼠标后继续运动
    38.                 $("#moveWindow").mouseout(function(){
    39.                     move = setInterval(moves,100);
    40.                 });
    41.                 //点击按钮关闭
    42.                 $("#removeMW").click(function(){
    43.                     $("#moveWindow").remove();
    44.                 });
    45.             })
    46.         </script>
    47.     </head>
    48.     <body>
    49.     </body>
    50. </html>
    复制代码
    效果展示:
    js悬窗
    帖子永久地址: 

    黑帽联盟 - 论坛版权1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
    2、本站所有主题由该帖子作者发表,该帖子作者与黑帽联盟享有帖子相关版权
    3、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者和黑帽联盟的同意
    4、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任
    5、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
    6、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
    7、黑帽联盟管理员和版主有权不事先通知发贴者而删除本文

    您需要登录后才可以回帖 登录 | 会员注册

    发布主题 !fastreply! 收藏帖子 返回列表 搜索
    回顶部