首页 > 其他分享 >js实现H5网页飘窗

js实现H5网页飘窗

时间:2023-04-27 16:01:58浏览次数:35  
标签:obj js nbsp var AdMoveConfig document 飘窗 Math H5

1.<div style="z-index: 1000; position: absolute; filter: Alpha(opacity = 90); width: 100px;padding: 10px; border: 1px solid #333" id="img" align="center">
<a href="javascript:void(0);" id="popInfo" style="text-decoration: none;color:#333">关于xxx的通知</a>
</div>

 

js代码

 

<script>
$(function() {
var content = '<div style="margin-left: 20px;margin-right: 20px;"><center>关于xxx的通知</center><br/>'
+ '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;按照xxx要求,可按照如下方式进行:<br/>'
+ '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;一、进行相关处理。<br/>'
+ '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;二、人员进行相关处理。<br/><br/>'
+ '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如有任何疑问,请联系相关客服人员<br/>'
+ '<br/>'
+ '<br/>'
+ '<p style="text-align: right;">xxx科技有限公司</p>'
+ '<p style="text-align: right;">x年10月1日</p></div>';

$('#popInfo').click(function() {
layer.open({
title : '通知',
type : 1,
area : [ '560px', '420px' ],
content : content
});
})
});

//关闭飘窗
function hidead() {
// document.getElementById("img1").style.display = "none";
document.getElementById("img").style.display = "none";
}

//飘窗开始
function addEvent(obj, evtType, func, cap) {
cap = cap || false;
if (obj.addEventListener) {
obj.addEventListener(evtType, func, cap);
return true;
} else if (obj.attachEvent) {
if (cap) {
obj.setCapture();
return true;
} else {
return obj.attachEvent("on" + evtType, func);
}
} else {
return false;
}
}
function getPageScroll() {
var xScroll, yScroll;
if (self.pageXOffset) {
xScroll = self.pageXOffset;
} else if (document.documentElement
&& document.documentElement.scrollLeft) {
xScroll = document.documentElement.scrollLeft;
} else if (document.body) {
xScroll = document.body.scrollLeft;
}
if (self.pageYOffset) {
yScroll = self.pageYOffset;
} else if (document.documentElement
&& document.documentElement.scrollTop) {
yScroll = document.documentElement.scrollTop;
} else if (document.body) {
yScroll = document.body.scrollTop;
}
arrayPageScroll = new Array(xScroll, yScroll);
return arrayPageScroll;
}
function GetPageSize() {
var xScroll, yScroll;
if (window.innerHeight && window.scrollMaxY) {
xScroll = document.body.scrollWidth;
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight) {
xScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else {
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}
var windowWidth, windowHeight;
if (self.innerHeight) {
windowWidth = self.innerWidth;
windowHeight = self.innerHeight;
} else if (document.documentElement
&& document.documentElement.clientHeight) {
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) {
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}
if (yScroll < windowHeight) {
pageHeight = windowHeight;
} else {
pageHeight = yScroll;
}
if (xScroll < windowWidth) {
pageWidth = windowWidth;
} else {
pageWidth = xScroll;
}
arrayPageSize = new Array(pageWidth, pageHeight, windowWidth,
windowHeight)
return arrayPageSize;
}
//AdMove
var AdMoveConfig = new Object();
AdMoveConfig.IsInitialized = false;
AdMoveConfig.ScrollX = 0;
AdMoveConfig.ScrollY = 0;
AdMoveConfig.MoveWidth = 0;
AdMoveConfig.MoveHeight = 0;
AdMoveConfig.Resize = function() {
var winsize = GetPageSize();
AdMoveConfig.MoveWidth = winsize[2];
AdMoveConfig.MoveHeight = winsize[3];
AdMoveConfig.Scroll();
}
AdMoveConfig.Scroll = function() {
var winscroll = getPageScroll();
AdMoveConfig.ScrollX = winscroll[0];
AdMoveConfig.ScrollY = winscroll[1];
}
addEvent(window, "resize", AdMoveConfig.Resize);
addEvent(window, "scroll", AdMoveConfig.Scroll);
function AdMove(id) {
if (!AdMoveConfig.IsInitialized) {
AdMoveConfig.Resize();
AdMoveConfig.IsInitialized = true;
}
var obj = document.getElementById(id);
obj.style.position = "absolute";
var W = AdMoveConfig.MoveWidth - obj.offsetWidth;
var H = AdMoveConfig.MoveHeight - obj.offsetHeight;
var x = W * Math.random(), y = H * Math.random();
var rad = (Math.random() + 1) * Math.PI / 6;
var kx = Math.sin(rad), ky = Math.cos(rad);
var dirx = (Math.random() < 0.5 ? 1 : -1), diry = (Math.random() < 0.5 ? 1 : -1);
var step = 1;
var interval;
this.SetLocation = function(vx, vy) {
x = vx;
y = vy;
}
this.SetDirection = function(vx, vy) {
dirx = vx;
diry = vy;
}
obj.CustomMethod = function() {
obj.style.left = (x + AdMoveConfig.ScrollX) + "px";
obj.style.top = (y + AdMoveConfig.ScrollY) + "px";
rad = (Math.random() + 1) * Math.PI / 6;
W = AdMoveConfig.MoveWidth - obj.offsetWidth;
H = AdMoveConfig.MoveHeight - obj.offsetHeight;
x = x + step * kx * dirx;
if (x < 0) {
dirx = 1;
x = 0;
kx = Math.sin(rad);
ky = Math.cos(rad);
}
if (x > W) {
dirx = -1;
x = W;
kx = Math.sin(rad);
ky = Math.cos(rad);
}
y = y + step * ky * diry;
if (y < 0) {
diry = 1;
y = 0;
kx = Math.sin(rad);
ky = Math.cos(rad);
}
if (y > H) {
diry = -1;
y = H;
kx = Math.sin(rad);
ky = Math.cos(rad);
}
}
this.Run = function() {
var delay = 25;//移动速度
interval = setInterval(obj.CustomMethod, delay);
obj.onmouseover = function() {
clearInterval(interval);
}
obj.onmouseout = function() {
interval = setInterval(obj.CustomMethod, delay);
}
}
}
var ad1 = new AdMove("img");
ad1.Run();
// var ad2 = new AdMove("img1");
// ad2.Run();
// ad2.SetLocation(50, 100)
// ad2.SetDirection(1, 1)
</script>

标签:obj,js,nbsp,var,AdMoveConfig,document,飘窗,Math,H5
From: https://www.cnblogs.com/zwvs/p/17359186.html

相关文章

  • ubuntu安装nodejs 14
    nodejs的每个大版本号都有相对应的源,比如这里的10.x.x版本的源是https://deb.nodesource.com/setup_10.x。所以在终端执行:curl-sLhttps://deb.nodesource.com/setup_14.x|sudo-Ebash-点击查看命令解释这个命令是用来添加Node.js14.x的仓库地址。具体来说,它通......
  • JS 数组方法
    一、创建数组1、使用数组字面量表示法vararr=[];//创建一个空数组vararr1=[1];//创建一个包含1项数据为1的数组vararr2=['a','b','c'];//创建一个包含3个字符串的数组2、使用Array构造函数vararr1newArray();//创建一个空数组vararr2=newArray(......
  • JS通过axios提交application/x-www-form-urlencoded类型的数据
    使用axios提交类型为application/x-www-form-urlencoded数据的正确姿势:letformData=newURLSearchParams()formData.append('param1',param1)formData.append('param2',param2)axios({url:'http://xxx.xxx.xxx.xxx/xxx',method:'......
  • JSON数据和JAVA对象之间的转换
    转:https://blog.csdn.net/qq_39975542/article/details/123947872方法1:利用阿里巴巴开源的fastjson包进行JAVA对象和JSON字符串进行转换。C++中的结构体在JAVA中用对象表示,目录结构如下: Peron类内容如下,对应C++中的Person结构体:packagecom.company;publicclassPerson{......
  • 使用 youth5201314:banner 库时出现 ViewPager 或 xandroid 报错
    使用youth5201314:banner这个库的1.4.10版本开发时,Build时报错找不到android.support.v4.view.ViewPager的类文件找不到androidx.fragment.app.Fragment的类文件需要在Project的gradle.properties配置文件中加一行,用于自动迁移第三方库android.enableJetifier=......
  • js 获取对象的key value,放到数组对象中
    letarr=[];if(res.data&&res.data.havePowerComCodes){for(const[key,value]ofObject.entries(res.data.havePowerComCodes)){constobj={......
  • golang1.6版本json包解析嵌套指针的问题小记
    指针的指针问题本地跑的好好的,测试环境跑的好好,预发布环境(准线上环境),跪了。起因就是:1a:=&struct{s:""}2json.Unmarshal([]byte{},&a)3fmt.Println(a.s)//报错行第一行代码进行&取地址,获得指针变量。第二行代码,进行json解析的时候,传入了&a, 指针的指针,a到了jso......
  • == vs === in js
    Equalitycomparisonsandsameness-JavaScript|MDN(mozilla.org)Looseequalityissymmetric:A==BalwayshasidenticalsemanticstoB==AforanyvaluesofAandB(exceptfortheorderofappliedconversions).Thebehaviorforperforminglooseequa......
  • vue中实际代码模拟JS中promise调接口的运行流程
    假设我们有一个需要调用接口的场景,我们可以使用Vue中的axios库来发送请求。以下是一个简单的例子://引入axios库importaxiosfrom'axios'//定义一个函数,用于发送请求functionfetchData(){//返回一个Promise对象returnnewPromise((resolve,reject)=>{//发......
  • 安装多个NodeJS windows上安装多个Nodejs版本 解决vue2/vue3同时运行
    第一步下载nvm-windowsnvm-windows下载地址:Github最新下载地址进入之后直接下载第二步安装NVM注意路径一定不要包含空格中文否则会报错注意安装路径一定不要包含空格中文否则会报错 点击安装之后如果之前安装了nodejs的话会提示希望nvm管理已安装node版本吗点击是......