最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

JS实现悬浮移动窗口(悬浮广告)的特效_javascript技巧

来源:动视网 责编:小采 时间:2020-11-27 21:07:42
文档

JS实现悬浮移动窗口(悬浮广告)的特效_javascript技巧

JS实现悬浮移动窗口(悬浮广告)的特效_javascript技巧:js方法: 代码如下: New Document window.onload=function(){ //写入 var oneInner = document.createElement(div); oneInner.setAttribute(style,background:#663398;position:absol
推荐度:
导读JS实现悬浮移动窗口(悬浮广告)的特效_javascript技巧:js方法: 代码如下: New Document window.onload=function(){ //写入 var oneInner = document.createElement(div); oneInner.setAttribute(style,background:#663398;position:absol


js方法:

代码如下:



New Document





window.onload=function(){
//写入
var oneInner = document.createElement("div");
oneInner.setAttribute("style","background:#663398;position:absolute;width:100px;height:100px;border:solid 3px #2F74A7;cursor:pointer;");
var oneButton = document.createElement("input");
oneButton.setAttribute("type","button");
oneButton.setAttribute("value","x");
if (oneButton.style.cssFloat)
{
oneButton.style.cssFloat="right"
}
else
{oneButton.style.styleFloat="right"}
oneInner.appendChild(oneButton);
document.body.appendChild(oneInner);

//定时器
var a1a = setInterval(moves,100);
//函数

var x = 10;
var y = 10;

function moves(){
var tops = oneInner.offsetTop
var lefts = oneInner.offsetLeft

if (lefts>=document.documentElement.clientWidth-oneInner.offsetWidth||lefts<=0)
{
x=-x
}

if (tops>=document.documentElement.clientHeight-oneInner.offsetHeight||tops<=0)
{
y=-y
}

tops+=y;
lefts+=x;

oneInner.style.top=tops+"px"
oneInner.style.left=lefts+"px"
}

//悬停停止
oneInner.onmouseover=function(){
clearInterval(a1a);
}
//放手继续运动
oneInner.onmouseout=function(){
a1a =setInterval(moves,100);
}
//删除
oneButton.onclick=function(){
document.body.removeChild(oneInner);
}
}




jquery方法:

代码如下:







$(function(){
//写入div
$("",{id:"moveWindow"}).css({width:"200px",height:"200px",border:"solid 3px #2F74A7",background:"#663398",position:"absolute",cursor:"pointer"}).appendTo("body");
//写入关闭按钮
$("",{id:"removeMW"}).css({width:"20px",height:"20px",background:"red",float:"right"}).appendTo("#moveWindow");
//定时器
var move = setInterval(moves,100);
var x= 10;
var y= 10;

function moves (){
var mw = $("#moveWindow").offset();
var lefts =mw.left;
var tops = mw.top;

if (lefts>=$(window).width()-$("#moveWindow").width()||lefts<=0)
{
x=-x
}

if (tops>=$(window).height()-$("#moveWindow").height()||tops<=0)
{
y=-y
}
lefts+=x;
tops+=y;

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

}
//悬停停止运动
$("#moveWindow").mouseover(function(){
clearInterval(move);
});
//移开鼠标后继续运动
$("#moveWindow").mouseout(function(){
move = setInterval(moves,100);
});
//点击按钮关闭
$("#removeMW").click(function(){
$("#moveWindow").remove();
});
})





基本思路:

1.页面加载完成之后向页面插入窗口,之后向窗口插入关闭按钮

2.使用setInterval()函数触发moves()函数开始动画

3.moves函数:首先获取当前窗口位置,之后随时间使窗口位移,每当位移到游览器边缘时反向运动

4.添加其他事件:鼠标悬停停止运动,鼠标离开继续运动,点击按钮关闭窗口

ps:不建议使用这个特效,影响用户体验

希望对你有所帮助!^_^!~~

文档

JS实现悬浮移动窗口(悬浮广告)的特效_javascript技巧

JS实现悬浮移动窗口(悬浮广告)的特效_javascript技巧:js方法: 代码如下: New Document window.onload=function(){ //写入 var oneInner = document.createElement(div); oneInner.setAttribute(style,background:#663398;position:absol
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top