Home >Web Front-end >JS Tutorial >Perfect gradient pop-up layer effect code implemented in pure JavaScript_javascript skills

Perfect gradient pop-up layer effect code implemented in pure JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:30:221093browse

As the title says, this article will use pure script JavaScript to implement a gradient pop-up layer. The idea is also very simple: use IFrame to cover it, then use a DIV to display the content of the pop-up layer, and finally use its built-in functions setInterval() and clearInterval( ) to implement gradients, the principle is very simple, but you need to pay attention to the precise control of DOM objects/elements. Since the code is relatively simple, I won’t go into details here. The following code passed the test in IE6 and FF3.5.
Script Flyout.js:

Copy code The code is as follows:

// 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.
Close Flyout
");
}
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" %>



Flyout Sample






Click me to test


























































































































最终效果图:
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn