PHP封装常用Javascript为JS类以便快速调用

<?
# $Id: ..php,v 0.1 p $

// 禁止直接访问该页面
if (basename($HTTP_SERVER_VARS['PHP_SELF']) == "js.class.php") {
     header("HTTP/1.0 404 Not Found");
}

# Purpose 封装了一些常用的Javascript代码,以便在PHP中快速调用

class JS{
     function JS(){}

     #返回上页
     # @param $step 返回的层数 默认为1
     function Back($step = -1){
         $msg = "history.go(".$step.");";
         JS::_Write($msg);
         JS::FreeResource();
         exit;
     }

    # 弹出警告的窗口
    # @param $msg 警告信息
     function Alert($msg){
         $msg = "alert(\"".$msg."\");";
         JS::_Write($msg);
     }

    # 写js
    # @param $msg
     function _Write($msg) {
         echo "<script language=\"javascript\">\n";
         echo $msg;
         echo "\n</SCRIPT>";
     }

     # 刷新当前页
     function Reload(){
         $msg = "location.reload();";
         JS::FreeResource();
         JS::_Write($msg);
         exit;
     }

     #刷新弹出父页
     function ReloadOpener(){
         $msg = "if (opener)     opener.location.reload();";
         JS::_Write($msg);
     }

     #跳转到url
     #@param $ 目标页
     function Goto($url){
         $msg = "location.href = ‘$url’;";
         JS::FreeResource();
         JS::_Write($msg);
         exit;
     }

     #关闭窗口
     function Close(){
         $msg = "window.close()";
         JS::FreeResource();
         JS::_Write($msg);
         exit;
     }

     #
     #@param $frm 表单名
     function Submit($frm){
         $msg = $frm.".submit();";
         JS::_Write($msg);
     }

    #关闭数据库连接
    function FreeResource(){
         // 数据库连接标志
         global $conn;
         if (is_resource($conn))
             @mysql_close($conn);
     }
}
?>

标签Tags:, , , , , , , , , , , , , , , , , , , ,

图片轮换类,IE7、OP、FF、Chrome测试通过

<>
<head><>新闻图片轮换类</>
<script type=”text/”>
var fs = new FadeSlideClass();
fs.Width = 430;
fs.Height = 250;
fs.AddImage(“http://www.5bay.cn/Test/Image/001.jpg“, “#”, “新闻标题”);
fs.AddImage(“http://www.5bay.cn/Test/Image/002.jpg“, “#”, “新闻标题”);
fs.AddImage(“http://www.5bay.cn/Test/Image/003.jpg“, “//g.cn”, “去Google”);
fs.AddImage(“http://www.5bay.cn/Test/Image/004.jpg“, “#”, “新闻标题”);
fs.AddImage(“http://www.5bay.cn/Test/Image/005.jpg“, “#”, “新闻标题”);
fs.RenderTo(“divStage”);

/* 以下代码构造轮换类 */
function FadeSlideClass()
{
this.Width = 450;
this.Height = 300;
this.BorderColor = “#ddd”;
this.CurrentColor = “green”;
this.Delay = 5000;
var divContainer = document.createElement(“div”);
var divImage = document.createElement(“div”);
var ulList = document.createElement(“ul”);
var me = this;
var arrImg = new Array;
this.Tag = divContainer;
this.AddImage = function(strImageUrl, strLink, strTitle)
{
   arrImg.push(arguments);
}
this.RenderTo = function(container)
{
   if(typeof container == “string”) container = document.getElementById(container) || container;
   if(typeof container == “string”) return setTimeout(function(){ me.RenderTo(container); }, 100);
   if(!arrImg.length) throw new Error(“尚未添加图片。”);
   var cssFloat = typeof ulList.style.cssFloat == “string” ? “cssFloat” : “styleFloat”;
   divContainer.style.width = this.Width + “px”;
   divContainer.style.height = this.Height + “px”;
   divImage.style.width = Math.round(this.Width * (arrImg.length – 1) / arrImg.length) + “px”;
   divImage.style.height = this.Height + “px”;
   divImage.style[cssFloat] = “left”;
   ulList.style.width = (this.Width – parseInt(divImage.style.width)) + “px”;
   ulList.style.height = this.Height + “px”;
   ulList.style.margin = “0px”;
   ulList.style.padding = “0px”;
   ulList.style[cssFloat] = “left”;
   divContainer.style.overflow = “hidden”;
   divImage.style.overflow = “hidden”;
   ulList.onmouseover = function(){ clearTimeout(me.Handler) };
   ulList.onmouseout = function(){ AutoPlay(); };
   InitImage(2);
   InitList(arrImg.length);
   container.appendChild(divContainer);
   divContainer.appendChild(divImage);
   divContainer.appendChild(ulList);
   LiMouseOver.call(ulList.firstChild);
   AutoPlay();
   this.AddImage = function(){ throw new Error(“该方法已过时。”); }
}
this.Display = function(imgThumb)
{
   var img = divImage.lastChild.lastChild;
   var opa = new OpacityClass(img);
   opa.Set(20);
   img.src = imgThumb.src;
   img.parentNode.href = imgThumb.parentNode.href;
   img.parentNode.title = imgThumb.parentNode.title;
   clearInterval(ulList.Handler);
   ulList.Handler = setInterval(function(){
    if(opa.Add(5) >= 100)
    {
     divImage.firstChild.lastChild.src = img.src;
     clearInterval(ulList.Handler);
    }
   }, 25);
}
function LiMouseOver()
{
   if(this == ulList.oldLi) return;
   if(ulList.oldLi) ulList.oldLi.style.borderColor = me.BorderColor;
   this.style.borderColor = me.CurrentColor;
   ulList.oldLi = this;
   me.Display(this.firstChild.firstChild);
}
function InitImage(count)
{
   if(–count) arguments.callee(count);
   var a = document.createElement(“a”);
   a.target = “_blank”;
   divImage.appendChild(a).style.position = “absolute”;
   var img = new Image;
   img.width = parseInt(divImage.style.width);
   img.height = me.Height;
   img.border = 0;
   if(!a.appendChild(img).filters) img.style.opacity = 1;
   else img.style.filter = “alpha(opacity=100)”;
}
function InitList(count)
{
   if(–count) InitList(count);
   var isIeNoDtd = document.compatMode != “CSS1Compat” && /msie/i.test(navigator.userAgent);
   var data = arrImg[count];
   var thumbWidth = parseInt(ulList.style.width);
   var thumbHeight = Math.round(me.Height / arrImg.length);
   var li = document.createElement(“li”);
   li.style.overflow = “hidden”;
   li.style.width = (isIeNoDtd ? thumbWidth : thumbWidth – 4) + “px”;
   li.style.height = (isIeNoDtd ? thumbHeight : thumbHeight – 4) + “px”;
   li.style.borderWidth = “2px”;
   li.style.borderStyle = “solid”;
   li.style.borderColor = me.BorderColor;
   var a = document.createElement(“a”);
   a.target = “_target”;
   a.href = data[1];
   a.title = data[2];
   var img = new Image;
   img.border = 0;
   img.width = thumbWidth;
   img.height = thumbHeight;
   img.src = data[0];
   img.style.verticalAlign = “top”;
   img.style.margin = “-2px”;
   a.appendChild(img);
   li.appendChild(a);
   li.style.listStyle = “none”;
   li.onmouseover = LiMouseOver;
   ulList.appendChild(li);
}
function AutoPlay()
{
   me.Handler = setTimeout(function()
   {
    LiMouseOver.call(ulList.oldLi.nextSibling || ulList.firstChild);
    AutoPlay();
   }, me.Delay);
}
function OpacityClass(tag)
{
   var isFilter = !!tag.filters;
   var obj = isFilter ? tag.filters.alpha : tag.style;
   this.Set = function(value)
   {
    obj.opacity = isFilter ? value : value / 100;
    return this;
   }
   this.Add = function(value)
   {
    var opa = isFilter ? obj.opacity : obj.opacity * 100;
    opa += value;
    if(opa > 100) opa = 100;
    obj.opacity = isFilter ? opa : opa / 100;
    return this;
   }
   this.valueOf = function()
   {
    return isFilter ? obj.opacity : obj.opacity * 100;
   }
}
}
</script>
</head>
<body>
<div id=”divStage”></div>
</body>
</html>

标签Tags:, , , , , , , , , , , , , , , ,

JS实现全景图效果360度旋转

< style="border-bottom: red 1px solid; position: absolute; border-left: red 1px solid; width: 500px; height: 190px; overflow: hidden; border-top: red 1px solid; cursor: hand; border-right: red 1px solid" id="1" onmouseout="change(1);" onmousemove="getpos()" onmouseover="clearTimeout(scrl);"><img onload="change(1);" alt="" src="http://www.jb51.net/upload/out500b.jpg" /></div>
<script>
var scrl,direction="right";
var x,xold,xdiff, i=1;

function change(start){
if(direction == "right"){
    if(div1.scrollTop+190 < div1.scrollHeight){
        div1.scrollTop += 190;
    }
    else{
        div1.scrollTop = 0;
    }
}
else{
    if(div1.scrollTop-190 > 0){
        div1.scrollTop -= 190;
    }
    else{
        div1.scrollTop = div1.scrollHeight;
    }
}
if(start) scrl = setTimeout("change(1)",100);
else clearTimeout(scrl);
}

function getpos(){
x = event.clientX;
xdiff = x – xold;
xold = x;
if(xdiff>0) direction = "right";
if(xdiff<0) direction = "left";
change(0);
}
</script>

标签Tags:, , , , ,

document.body.scrollTop为0

今天,在作一个网页特效的时候,
遇到了一个状况,
因為要作的功能是,卷动瀏览器的卷轴后,
广告图片会跟著滑动.
而这功能,在网路上已经有很多范例了,
所以特别挑了一个简短而且在IE跟Firefox上都可以用的来,
稍微修改一下,写个范例测试,没有问题就想说可以开始套了,
结果没想到将那个片段套在程式裡以后,却不会动…
所以只好重头debug了.
debug到后来才发现,
原本的测试网页中使用的document.body.scrollTop是正常的,
但是套用到php的页面时,
不管怎麼卷动,document.body.scrollTop都是0. (更多…)

标签Tags:, , , , , , , , ,

GOOGLE的个性化主页拖搁功能实例

(更多…)

标签Tags:, , , ,

js表单输入判断

(更多…)

标签Tags:, , ,

超酷右下浮出广告窗口代码

(更多…)

标签Tags:, ,

Ajax类,支持事件,容器,多种操作方式,兼容ie,firefox

(更多…)

标签Tags:, , , , , , ,

JS扩展函数集合

(更多…)

标签Tags:, , ,

关于Cookie中的Path和domain

– 路径。指定与cookie关联的WEB页。值可以是一个目录,或者是一个路径。如果http://www.5bay.cn/test/index. 建立了一个cookie,那么在http://www.5bay.cn/test/目录里的所有页面,以及该目录下面任何子目录里的页面都可以访问这个cookie。这就是说,在http://www.5bay.cn/test/test2/test3 里的任何页面都可以访问http://www.5bay.cn/test/index.html建立的cookie。但是,如果http://www.5bay.cn/test/ 需要访问http://www.5bay.cn/test/index.html设置的cookes,该怎么办?这时,我们要把cookies的path属性设置成“/”。在指定路径的时候,凡是来自同一服务器,URL里有相同路径的所有WEB页面都可以共享cookies。 (更多…)

标签Tags:, , ,