html 原生弹出框,使用原生JS实现弹出层特效_javascript技巧

5268f80b9b1e01f982625ef6fac83ca1.png

创建遮罩层

代码如下:

_createCover: function() {

var newMask = document.createElement("div");

newMask.id = this._mark;

newMask.style.position = "absolute";

newMask.style.zIndex = "100";

_scrollWidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);

_scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);

newMask.style.width = _scrollWidth + "px";

newMask.style.height = _scrollHeight + "px";

newMask.style.top = "0px";

newMask.style.left = "0px";

newMask.style.background = "#000";

newMask.style.filter = "alpha(opacity=50)";

newMask.style.opacity = "0.50";

newMask.style.display = 'none';

document.body.appendChild(newMask);

this._cover = newMask;

}

新建弹出层

代码如下:

_createFloater: function(html) {

var newDiv = document.createElement("div");

newDiv.id = this._id;

newDiv.style.position = "absolute";

newDiv.style.zIndex = "9999";

newDivWidth = 400;

newDivHeight = 200;

newDiv.style.width = newDivWidth + "px";

newDiv.style.height = newDivHeight + "px";

newDiv.style.top = (document.body.scrollTop + document.body.clientHeight/2 - newDivHeight/2) + "px";

newDiv.style.left = (document.body.scrollLeft + document.body.clientWidth/2 - newDivWidth/2) + "px";

newDiv.style.padding = "5px";

newDiv.style.display = 'none';

newDiv.innerHTML = html;

document.body.appendChild(newDiv);

this._floater = newDiv;

}

调节弹层位置

代码如下:

addjustPosition: function() {

this._floater.style.top = (document.body.scrollTop + document.body.clientHeight/2 - newDivHeight/2) + "px";

this._floater.style.left = (document.body.scrollLeft + document.body.clientWidth/2 - newDivWidth/2) + "px";

}

屏幕滚动事件时调整位置

代码如下:

this._fS = BindAsEventListener(this, this.addjustPosition);

addEventHandler(window, "scroll", this._fS);

// 隐藏后需

removeEventHandler(window, "scroll", this._fS);

完整代码

代码如下:

var Floater = (function(){

var me = Class.create();

me.prototype = {

initialize: function(options) {

this._fS = BindAsEventListener(this, this.addjustPosition);

this.setOptions(options);

},

setOptions: function(options) {

this.options = options || {};

this._id = options.id;

this._mark = 'mark';

},

show: function(html,options) {

options = options || {};

if(!this._cover){

this._createCover();

}

if(!this._floater){

this._createFloater(html);

}

if(options.saveOpt){

this._saveOption = options.saveOpt;

this.bindSaveEvent();

}

this._bindScrollEvent();

this.addjustPosition();

this._floater.style.display = '';

this._cover.style.display = '';

this.isShow = true;

},

insert: function(html,opts,att){

var _e = document.createElement("div"), _t;

_e.innerHTML = html;

for(var k in opts){

_e[k] = opts[k];

}

_t = this._floater.querySelector('['+att+']');

if(_t){

_t.appendChild(_e);

}

},

getFloater: function(){

if(this._floater){

return this._floater;

}

},

//遮罩层

_createCover: function() {

var newMask = document.createElement("div");

newMask.id = this._mark;

newMask.style.position = "absolute";

newMask.style.zIndex = "100";

_scrollWidth = Math.max(document.body.scrollWidth,document.documentElement.scrollWidth);

_scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);

newMask.style.width = _scrollWidth + "px";

newMask.style.height = _scrollHeight + "px";

newMask.style.top = "0px";

newMask.style.left = "0px";

newMask.style.background = "#000";

newMask.style.filter = "alpha(opacity=50)";

newMask.style.opacity = "0.50";

newMask.style.display = 'none';

document.body.appendChild(newMask);

this._cover = newMask;

},

//新弹出层

_createFloater: function(html) {

var newDiv = document.createElement("div");

newDiv.id = this._id;

newDiv.style.position = "absolute";

newDiv.style.zIndex = "9999";

newDivWidth = 400;

newDivHeight = 200;

newDiv.style.width = newDivWidth + "px";

newDiv.style.height = newDivHeight + "px";

newDiv.style.top = (document.body.scrollTop + document.body.clientHeight/2 - newDivHeight/2) + "px";

newDiv.style.left = (document.body.scrollLeft + document.body.clientWidth/2 - newDivWidth/2) + "px";

newDiv.style.padding = "5px";

newDiv.style.display = 'none';

newDiv.innerHTML = html;

document.body.appendChild(newDiv);

this._floater = newDiv;

},

//弹出层滚动居中

addjustPosition: function() {

this._floater.style.top = (document.body.scrollTop + document.body.clientHeight/2 - newDivHeight/2) + "px";

this._floater.style.left = (document.body.scrollLeft + document.body.clientWidth/2 - newDivWidth/2) + "px";

},

bindSaveEvent: function() {

this._saveElem = this._floater.querySelector('['+this._saveOption.elem+']');

if(this._saveElem){

addEventHandler(this._saveElem, "click", this._saveOption.handler);

}

},

_bindScrollEvent: function() {

addEventHandler(window, "scroll", this._fS);

},

hide: function() {

this.isShow = false;

this.destory();

},

destory: function() {

removeEventHandler(window, "scroll", this._fS);

if(this._saveElem){

removeEventHandler(this._saveElem, "click", this._saveOption.handler);

}

if (this._cover){

document.body.removeChild(this._cover);

}

if (this._floater){

document.body.removeChild(this._floater);

}

this._cover = null;

this._floater = null;

}

};

return me;

})();

1428d0e076c3959ab11d28a39bc84fab.png

5268f80b9b1e01f982625ef6fac83ca1.png

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用jQuery原生可以很容易实现可拖拽放大缩小的框。首先,需要引入jQuery库,在HTML中创建框的结构,并设置好初始样式。然后使用jQuery的事件绑定方法,为框的标题栏添加鼠标按下事件,实现拖拽功能。接着,为框的放大缩小按钮添加点击事件,通过改变框的宽高和位置实现放大缩小效果。具体实现步骤如下: 1. 引入jQuery库: ```html <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> ``` 2. 创建框结构: ```html <div class="popup"> <div class="popup-title">框标题</div> <div class="popup-content"> <!-- 框内容 --> </div> <div class="popup-controls"> <button class="btn-zoom-in">放大</button> <button class="btn-zoom-out">缩小</button> </div> </div> ``` 3. 设置初始样式: ```css .popup { position: absolute; top: 50px; left: 50px; width: 200px; height: 150px; border: 1px solid #000; background-color: #fff; } .popup-title { cursor: move; } ``` 4. 实现拖拽功能: ```javascript $('.popup-title').on('mousedown', function(e) { var offsetX = e.clientX - $(this).offset().left; var offsetY = e.clientY - $(this).offset().top; $(document).on('mousemove', function(e) { $('.popup').offset({ top: e.clientY - offsetY, left: e.clientX - offsetX }); }); $(document).on('mouseup', function() { $(document).off('mousemove'); $(document).off('mouseup'); }); }); ``` 5. 实现放大缩小功能: ```javascript $('.btn-zoom-in').on('click', function() { var width = $('.popup').width(); var height = $('.popup').height(); $('.popup').css({ width: width * 1.2, height: height * 1.2 }); }); $('.btn-zoom-out').on('click', function() { var width = $('.popup').width(); var height = $('.popup').height(); $('.popup').css({ width: width * 0.8, height: height * 0.8 }); }); ``` 通过以上步骤,就可以实现一个带拖拽、放大和缩小功能的框。当用户鼠标点击框标题栏后,即可拖动整个框;点击放大缩小按钮后,即可实现框的放大和缩小效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值