如题,本文将使用纯粹的脚本JavaScript实现渐变的弹出层,其想法也非常简单:利用IFrame实现遮盖,再借助一个DIV用于展现弹出层的内容,最后再使用其内置函数setInterval()和clearInterval()实现渐变,原理非常简单,只不过要注意对DOM对象/元素的精确控制。由于代码比较简单,在此就不再赘述。以下代码在IE6+,FF3.5+测试通过。
脚本Flyout.js:
复制代码 代码如下:
// JScript File
if (navigator.userAgent.toLowerCase().indexOf('msie') > -1) {
window.isIE = true;
window.isIE6 = navigator.appVersion.indexOf("MSIE 6.0;") > -1;
window.isIE7 = navigator.appVersion.indexOf("MSIE 7.0;") > -1;
window.isIE8 = navigator.appVersion.indexOf("MSIE 8.0;") > -1;
}
var $ = function(objID) { return document.getElementById(objID) };
var _flyout;
var _fadeTimer;
function showflyout(divWidth, divHeight, paddingWidth) {
var flyout = function() {
}
flyout.prototype = {
clientWidth: document.documentElement.clientWidth,
clientHeight: document.documentElement.clientHeight,
scrollWidth: document.documentElement.scrollWidth,
scrollHeight: document.documentElement.scrollHeight,
iframeID: "envelopIframe",
divID: "popupcontent",
iframebgColor: "#888888",
show: function(divContent) {
//Create envelop iframe
cssText = "position:absolute; z-index:100; background-color:#888888; border-width:0px; filter:alpha(opacity=0); opacity:0.0;";
cssText += "left:0px;";
cssText += "top:0px;";
cssText += "width:" + this.scrollWidth + "px;";
cssText += "height:" + this.scrollHeight + "px;";
this.create("iframe", this.iframeID, cssText, "Cppl_IFrameSrc.htm", "");
_fadeTimer = setInterval(function() { fadeIframe("envelopIframe", 0.05, 0, 0.5, true) }, 5);
//Create flyout
var cssText = "";
cssText += "display:block; _position:absolute; position:fixed; z-index:101; border:solid 1px Gray; background-color:white;";
cssText += "left:" + (this.clientWidth - divWidth - paddingWidth) / 2 + "px;";
cssText += "top:" + (this.clientHeight - divHeight - paddingWidth) / 2 + "px;";
cssText += "width:" + (divWidth + paddingWidth) + "px;";
cssText += "height: " + (divHeight + paddingWidth) + "px;";
this.create("div", this.divID, cssText, "", divContent);
},
create: function(type, id, csstext, iframesrc, innerhtml) {
var obj = document.createElement(type);
if (iframesrc.length > 0) {
obj.src = iframesrc;
}
obj.setAttribute("id", id);
obj.style.cssText = csstext;
if (innerhtml.length > 0) {
obj.innerHTML = innerhtml;
}
document.body.appendChild(obj);
if (iframesrc.length > 0) {
if (window.isIE) {
window.envelopIframe.document.bgColor = this.iframebgColor;
}
}
},
close: function() {
var objIframe = document.getElementById(this.iframeID);
var objDiv = document.getElementById(this.divID);
if (objIframe && objDiv) {
document.body.removeChild(objIframe);
document.body.removeChild(objDiv);
}
},
onresize: function() {
var objIframe = document.getElementById(this.iframeID);
var objDiv = document.getElementById(this.divID);
if (objIframe && objDiv) {
objIframe.style.width = document.documentElement.scrollWidth + "px";
objIframe.style.height = document.documentElement.scrollHeight + "px";
objDiv.style.left = (document.documentElement.clientWidth - divWidth) / 2 + "px";
objDiv.style.top = (document.documentElement.clientHeight - divHeight) / 2 + "px";
}
},
onscroll: function() {
var objDiv = document.getElementById(this.divID);
if (objDiv) {
objDiv.style.left = (document.documentElement.clientWidth - divWidth) / 2 + document.documentElement.scrollLeft + "px";
objDiv.style.top = (document.documentElement.clientHeight - divHeight) / 2 + document.documentElement.scrollTop + "px";
}
}
};
_flyout = new flyout();
_flyout.show("This is a flyout.<div onclick=\"javascript:closeflyout()\">Close Flyout</div>");
}
function closeflyout() {
clearInterval(_fadeTimer);
_fadeTimer = setInterval(function() { fadeIframe("envelopIframe", 0.05, 0, 0.5, false) }, 5);
}
window.onresize = function() {
if (_flyout) {
_flyout.onresize();
}
};
window.onscroll = function() {
if (_flyout && isIE6) {
_flyout.onscroll();
}
};
function fadeIframe(objId, speed, minOpacity, maxOpacity, flag) {
var dialog = $(objId);
if (dialog) {
var value;
if (flag) {
if (parseFloat(dialog.style.opacity) <= maxOpacity) {
value = parseFloat(dialog.style.opacity) + speed;
dialog.style.filter = 'alpha(opacity=' + value * 100 + ')';
dialog.style.opacity = '' + value + '';
}
else {
clearInterval(_fadeTimer);
}
}
else {
if (parseFloat(dialog.style.opacity) >= minOpacity) {
value = parseFloat(dialog.style.opacity) - speed;
dialog.style.filter = 'alpha(opacity=' + value * 100 + ')';
dialog.style.opacity = '' + value + '';
}
else {
clearInterval(_fadeTimer);
if (_flyout) {
_flyout.close();
}
}
}
}
}
调用ASPX代码:
复制代码 代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Flyout Sample</title>
<script type="text/javascript" src="/UploadFiles/2021-04-02/Flyout.js"></head>
<body>
<form id="form1" runat="server">
<div>
<div onclick="javascript:showflyout(700, 300, 10);">
Click me to test
</div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>
最终效果图:
脚本Flyout.js:
复制代码 代码如下:
// JScript File
if (navigator.userAgent.toLowerCase().indexOf('msie') > -1) {
window.isIE = true;
window.isIE6 = navigator.appVersion.indexOf("MSIE 6.0;") > -1;
window.isIE7 = navigator.appVersion.indexOf("MSIE 7.0;") > -1;
window.isIE8 = navigator.appVersion.indexOf("MSIE 8.0;") > -1;
}
var $ = function(objID) { return document.getElementById(objID) };
var _flyout;
var _fadeTimer;
function showflyout(divWidth, divHeight, paddingWidth) {
var flyout = function() {
}
flyout.prototype = {
clientWidth: document.documentElement.clientWidth,
clientHeight: document.documentElement.clientHeight,
scrollWidth: document.documentElement.scrollWidth,
scrollHeight: document.documentElement.scrollHeight,
iframeID: "envelopIframe",
divID: "popupcontent",
iframebgColor: "#888888",
show: function(divContent) {
//Create envelop iframe
cssText = "position:absolute; z-index:100; background-color:#888888; border-width:0px; filter:alpha(opacity=0); opacity:0.0;";
cssText += "left:0px;";
cssText += "top:0px;";
cssText += "width:" + this.scrollWidth + "px;";
cssText += "height:" + this.scrollHeight + "px;";
this.create("iframe", this.iframeID, cssText, "Cppl_IFrameSrc.htm", "");
_fadeTimer = setInterval(function() { fadeIframe("envelopIframe", 0.05, 0, 0.5, true) }, 5);
//Create flyout
var cssText = "";
cssText += "display:block; _position:absolute; position:fixed; z-index:101; border:solid 1px Gray; background-color:white;";
cssText += "left:" + (this.clientWidth - divWidth - paddingWidth) / 2 + "px;";
cssText += "top:" + (this.clientHeight - divHeight - paddingWidth) / 2 + "px;";
cssText += "width:" + (divWidth + paddingWidth) + "px;";
cssText += "height: " + (divHeight + paddingWidth) + "px;";
this.create("div", this.divID, cssText, "", divContent);
},
create: function(type, id, csstext, iframesrc, innerhtml) {
var obj = document.createElement(type);
if (iframesrc.length > 0) {
obj.src = iframesrc;
}
obj.setAttribute("id", id);
obj.style.cssText = csstext;
if (innerhtml.length > 0) {
obj.innerHTML = innerhtml;
}
document.body.appendChild(obj);
if (iframesrc.length > 0) {
if (window.isIE) {
window.envelopIframe.document.bgColor = this.iframebgColor;
}
}
},
close: function() {
var objIframe = document.getElementById(this.iframeID);
var objDiv = document.getElementById(this.divID);
if (objIframe && objDiv) {
document.body.removeChild(objIframe);
document.body.removeChild(objDiv);
}
},
onresize: function() {
var objIframe = document.getElementById(this.iframeID);
var objDiv = document.getElementById(this.divID);
if (objIframe && objDiv) {
objIframe.style.width = document.documentElement.scrollWidth + "px";
objIframe.style.height = document.documentElement.scrollHeight + "px";
objDiv.style.left = (document.documentElement.clientWidth - divWidth) / 2 + "px";
objDiv.style.top = (document.documentElement.clientHeight - divHeight) / 2 + "px";
}
},
onscroll: function() {
var objDiv = document.getElementById(this.divID);
if (objDiv) {
objDiv.style.left = (document.documentElement.clientWidth - divWidth) / 2 + document.documentElement.scrollLeft + "px";
objDiv.style.top = (document.documentElement.clientHeight - divHeight) / 2 + document.documentElement.scrollTop + "px";
}
}
};
_flyout = new flyout();
_flyout.show("This is a flyout.<div onclick=\"javascript:closeflyout()\">Close Flyout</div>");
}
function closeflyout() {
clearInterval(_fadeTimer);
_fadeTimer = setInterval(function() { fadeIframe("envelopIframe", 0.05, 0, 0.5, false) }, 5);
}
window.onresize = function() {
if (_flyout) {
_flyout.onresize();
}
};
window.onscroll = function() {
if (_flyout && isIE6) {
_flyout.onscroll();
}
};
function fadeIframe(objId, speed, minOpacity, maxOpacity, flag) {
var dialog = $(objId);
if (dialog) {
var value;
if (flag) {
if (parseFloat(dialog.style.opacity) <= maxOpacity) {
value = parseFloat(dialog.style.opacity) + speed;
dialog.style.filter = 'alpha(opacity=' + value * 100 + ')';
dialog.style.opacity = '' + value + '';
}
else {
clearInterval(_fadeTimer);
}
}
else {
if (parseFloat(dialog.style.opacity) >= minOpacity) {
value = parseFloat(dialog.style.opacity) - speed;
dialog.style.filter = 'alpha(opacity=' + value * 100 + ')';
dialog.style.opacity = '' + value + '';
}
else {
clearInterval(_fadeTimer);
if (_flyout) {
_flyout.close();
}
}
}
}
}
调用ASPX代码:
复制代码 代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Flyout Sample</title>
<script type="text/javascript" src="/UploadFiles/2021-04-02/Flyout.js"></head>
<body>
<form id="form1" runat="server">
<div>
<div onclick="javascript:showflyout(700, 300, 10);">
Click me to test
</div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>
最终效果图:
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]