飘窗原生js效果
css:
.close {
width: 30px;
height: 20px;
background: white;
position: absolute;
right: 0;
top: 0;
z-index: 999;
font-size: 12px;
text-align: center;
line-height: 20px;
cursor: pointer;
font-family: '微软雅黑';
}
js:【修改了一些bug】
window.onload = function () {
/*
参数
1. 创建飘窗,填写飘窗的id;
2. 选择该飘窗是否显示,true显示,false不显示,默认为false,不显示;
3. 该飘窗的配置信息, xPos,yPos 飘窗的起始位置,默认为左上角,
imgWidth,imgHeight 飘窗的大小,默认为w: 310,h: 200,
href 要跳转的链接,默认不跳转,
imgSrc 图片的链接,默认为空。
*/
var fl1 = new FloatWindow('win', true, {
xPos: 100,
yPos: 100,
href: 'http://websong.wang',
imgSrc: 'https://bpic.588ku.com/art_origin_min_pic/18/07/08/02ced7cd66e3a1341391af9ceb90ec62.jpg'
});
}
var FloatWindow = function (ele, flag, config) {
flag = flag ? true : false;
this.opt = this.extend({
xPos: 0,
yPos: 0,
imgWidth: 100,
imgHeight: 100,
href: 'javascript:void(0)',
imgSrc: '',
step: 1,
height: 0,
Hoffset: 0,
Woffset: 0,
xon: 0,
yon: 0
}, config);
var html = '<span id="' + ele + '" class="floatWin" style="position:absolute; z-index: 1000;" >' +
'<a href="' + this.opt.href + '" target="_blank">' +
'<img src="' + this.opt.imgSrc + '" border="0">' +
'</a>' +
'<div class="close">关闭</div>' +
'</span>';
var div = document.createElement('div');
div.innerHTML = html;
if (flag) {
document.body.appendChild(div);
} else {
return;
}
if (document.querySelector('#'+ele).length <= 0) {
return;
}
this.ele = document.querySelector('#'+ele);
this.interval;
this.delay = 10;
this.ele.querySelector('img').style['width'] = this.opt.imgWidth + 'px';
this.ele.querySelector('img').style['height']= this.opt.imgHeight + 'px';
var _self = this;
this.ele.onmouseout = function () {
_self.start();
}
this.ele.onmouseover = function () {
_self.stop();
}
this.ele.onclick = function (e) {
if (e.target.className === 'close')
_self.stop();
document.body.removeChild(div);
}
changePos = function (ele, moveCfg) {
width = document.documentElement.clientWidth || document.body.clientWidth;
height = document.documentElement.clientHeight || document.body.clientHeight;
Hoffset = ele.offsetHeight;
Woffset = ele.offsetWidth;
if (moveCfg.yon) {
moveCfg.yPos = moveCfg.yPos + moveCfg.step;
} else {
moveCfg.yPos = moveCfg.yPos - moveCfg.step;
}
if (moveCfg.yPos < 0) {
moveCfg.yon = 1;
moveCfg.yPos = 0;
}
if (moveCfg.yPos >= (height - Hoffset)) {
moveCfg.yon = 0;
moveCfg.yPos = (height - Hoffset);
}
if (moveCfg.xon) {
moveCfg.xPos = moveCfg.xPos + moveCfg.step;
} else {
moveCfg.xPos = moveCfg.xPos - moveCfg.step;
}
if (moveCfg.xPos < 0) {
moveCfg.xon = 1;
moveCfg.xPos = 0;
}
if (moveCfg.xPos >= (width - Woffset)) {
moveCfg.xon = 0;
moveCfg.xPos = (width - Woffset);
}
ele.style.left = moveCfg.xPos + document.body.scrollLeft + "px";
ele.style.top = moveCfg.yPos + document.documentElement.scrollTop + "px";
}
this.start();
}
FloatWindow.prototype.start = function () {
var that = this;
this.ele.visibility = 'visible';
this.interval = setInterval(function () {
changePos(that.ele, that.opt);
}, this.delay);
}
FloatWindow.prototype.stop = function () {
clearInterval(this.interval)
}
FloatWindow.prototype.extend = function (o, e) {
for (var key in e) {
if (e[key]) {
o[key] = e[key];
}
}
return o;
}
飘窗原生js效果的更多相关文章
- js 字符串分割成字符串数组 遍历数组插入指定DOM里 原生JS效果
使用的TP3.2 JS字符串分割成字符串数组 var images='{$content.pictureurl} ' ;结构是这样 attachment/picture/uploadify/20141 ...
- 原生js实现单屏滚动
类似于fullpage的单屏滚动,使用原生JS实现,不依赖任何js库: css: html,body {height:100%;} body {margin:0px;} div {height:100 ...
- 原生JS实现"旋转木马"效果的图片轮播插件
一.写在最前面 最近都忙一些杂七杂八的事情,复习软考.研读经典...好像都好久没写过博客了... 我自己写过三个图片轮播,一个是简单的原生JS实现的,没有什么动画效果的,一个是结合JQuery实现的, ...
- React.js实现原生js拖拽效果及思考
一.起因&思路 不知不觉,已经好几天没写博客了...近来除了研究React,还做了公司官网... 一直想写一个原生js拖拽效果,又加上近来学react学得比较嗨.所以就用react来实现这个拖 ...
- 原生JS封装简单动画效果
原生JS封装简单动画效果 一致使用各种插件,有时候对原生JS陌生了起来,所以决定封装一个简单动画效果,熟悉JS原生代码 function animate(obj, target,num){ if(ob ...
- 原生JS实现分页效果2.0(新增了上一页和下一页,添加当前元素样式)
虽然写的很烂,但至少全部都是自己写的,因为这个没有固定的顺序,所以就没有封装,如果你技术好的话,可以你写的分享给我,谢谢. <!DOCTYPE html><html lang=&qu ...
- 原生JS实现分页效果1.0
不太完整,写的太急,等等加上完整注释,写起来还是有些难度的,写的有点水,后面再改进改进. <!DOCTYPE html><html lang="en">&l ...
- 原生js动画效果(源码解析)
在做页面中,多数情况下都会遇到页面上做动画效果,大部分都是用jquery来实现动画,今天正好看到一篇原生js实现动画效果的代码,特分享在此. 原文地址:http://www.it165.net/pro ...
- 利用tween,使用原生js实现模块回弹动画效果
最近有一个需求,就是当屏幕往下一定像素时,下方会有一个隐藏的模块马上显现出来,向上运动后带有回弹效果.然后屏幕滚回去时这个模块能够原路返回 其实这个效果css3就可以很轻松实现,但是公司要求最低兼容i ...
随机推荐
- java 注解详解
先引用一下百度百科的名词解析: 定义:注解(Annotation),也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举是在同一个层次.它可以声明在包.类.字段. ...
- Prefetch count--预取数量
一.前言 前面提到如果有多个消费者同时订阅同一个Queue中的消息,Queue中的消息会被平摊给多个消费者.这时如果每个消息的处理时间不同,就有可能会导致某些消费者一直在忙,而另外一些消费者很快就处理 ...
- shell 脚本判断linux 的发行版本
原文vi ./Get_Dist_Name.sh #!/bin/bash Get_Dist_Name() { if grep -Eqii "CentOS" /etc/issue || ...
- ZooKeeper在线迁移
在至少有一个Leader存在的前提下,进行Zookeeper的在线增量.在线减量.在线迁移 在全过程中ZooKeeper不停止服务 注意事项 首先,当我们要从3台扩充到5台时,应保证集群不停止服务. ...
- Google Map API 应用实例说明
目录 Google Map API 1基础知识 1.1 Google 地图 API 概念 1.2 Google 地图的"Hello, World" 1.2.1 加载 Google ...
- Javascript动态绑定
<div onclick="test()"></div> <script> function test(){ //code } </scr ...
- ① 设计模式的艺术-07.适配器(Adapter)模式
什么是适配器模式? 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 模式中的角色 目标接口(Target):客户所期待的接口 ...
- 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)
题目链接:http://codevs.cn/problem/4163/ 题目:
- 可怕的npm蠕虫
https://hackernoon.com/im-harvesting-credit-card-numbers-and-passwords-from-your-site-here-s-how-9a8 ...
- php常用代码段
点击换验证码 <a href=" src="{:U('Reglog/vcode')}" /></a> TP上一条下一条 $prev=$artica ...