自定义的水平滚动条

<html>
<head>
    <title>网页特效|http://www.jscode.cn|---用图片替代滚动条的方法</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <style type="text/css">
        #scrollerContent {POSITION: absolute}
        body {font-size:9pt;color:#660000;text-decoration: none}
    </style>
<script>
var leftH = 13;//向上的箭头的高度
var leftW = 9; //向上的箭头的宽度
var rightH = 13;//向下的箭头的高度
var rightW = 9;//向下的箭头的宽度
var dragH = 9; //滚动条的高度
var dragW = 26; //滚动条的宽度
var scrollW =78; //滚动体的高度
var speed =9; //滚动的速度
var dom = document.getElementById ? true:false;
var nn4 = document.layers ? true:false;
var ie4 = document.all ? true:false;
var mouseY;
var mouseX;
var clickLeft = false;
var clickRight = false;
var clickDrag = false;
var clickAbove = false;
var clickBelow = false;
var timer = setTimeout("",500);
var leftL;
var leftT;
var rightL;
var rightT;
var dragL;
var dragT;
var rulerL;
var rulerT;
var contentL;
var contentW;
var contentClipW;
var scrollLength;
var startX;
function down(e)
{
    //单击左健时处理
    if((document.layers && e.which!=1) || (document.all && event.button!=1))
         return true;
    getMouse(e);
    startX = (mouseX - dragL);
    if(mouseX >= leftL && (mouseX <= (leftL + leftW)) && mouseY >= leftT && (mouseY <= (leftT + leftH)))   //单击向左的箭头
    {
        clickLeft = true;
        return scrollForLeft();
    }
    else
        if(mouseX >= rightL && (mouseX <= (rightL + rightW)) && mouseY >= rightT && (mouseY <= (rightT + rightH)))    //单击向右的箭头
        {
            clickRight = true;
            return scrollForRight();
        }
    else
        if(mouseX >= dragL && (mouseX <= (dragL + dragW)) && mouseY >= dragT && (mouseY <= (dragT + dragH)))    //滚动条内不做操作
        {
            clickDrag = true;
            return false;
        }
    else
        if(mouseY >= dragT && (mouseY <= (dragT + dragH)) && mouseX >= rulerL && (mouseX <= (rulerL + scrollW)))    //滚动体内
        {
            if(mouseX < dragL)      //向左滚
            {
                clickAbove = true;
                clickLeft = true;
                return scrollForLeft();
            }
            else                    //向右滚
            {
                clickBelow = true;
                clickRight = true;
                return scrollForRight();
            }
        }
        else
        {
            return true;
        }
}

function move(e)
{
    if(clickDrag && contentW > contentClipW)        //有滚动条时执行
    {
        getMouse(e);
        dragL = (mouseX - startX);
        if(dragL < (rulerL))
            dragL = rulerL;
        if(dragL > (rulerL + scrollW - dragW))
            dragL = (rulerL + scrollW - dragW);
        contentL = ((dragL - rulerL)*(1/scrollLength));
        contentL = eval('-' + contentL);
        moveTo();
        if(ie4)
            return false;
    }
}

//停止滚动
function up()
{
    clearTimeout(timer);
    clickLeft = false;
    clickRight = false;
    clickDrag = false;
    clickAbove = false;
    clickBelow = false;
    return true;
}

function getL()
{
    if(ie4)
        contentL = document.all.scrollerContent.style.pixelLeft;
    else
        if(nn4)
            contentL = document.scrollerContentClip.document.scrollerContent.left;
        else
            if(dom)
                contentL = parseInt(document.getElementById("scrollerContent").style.left);
}

function getMouse(e)
{
    if(ie4)
    {
        mouseY = event.clientY + document.body.scrollTop;
        mouseX = event.clientX + document.body.scrollLeft;
    }
    else if(nn4 || dom)
    {
        mouseY = e.pageY;
        mouseX = e.pageX;
    }
}

//移动内容
function moveTo()
{
    if(ie4)
    {
        document.all.scrollerContent.style.left = contentL;
        document.all.ruler.style.left = dragL;
        document.all.drag.style.left = dragL;
    }
    else
    if(nn4)
    {
        document.scrollerContentClip.document.scrollerContent.top = contentL;
        document.ruler.left = dragL;
        document.drag.left = dragL;
    }
    else
        if(dom)
        {
            document.getElementById("scrollerContent").style.left = contentL + "px";
            document.getElementById("drag").style.left = dragL + "px";
            document.getElementById("ruler").style.left = dragL + "px";
        }
}

function scrollForLeft()
{
    getL();
    if(clickAbove)
    {
        if(dragL <= (mouseX-(dragW/2)))     //移到指定的位置退出
            return up();
    }
    if(clickLeft)
    {
        if(contentL < 0)
        {
            dragL = dragL - (speed*scrollLength);
            if(dragL < (rulerL))
                dragL = rulerL;
            contentL = contentL + speed;
            if(contentL > 0)
                contentL = 0;
            moveTo();       //移动一个滚动距离
            timer = setTimeout("scrollForLeft()",25);
        }
    }
    return false;
}

function scrollForRight()
{
    getL();
    if(clickBelow)
    {
        if(dragL >= (mouseX-(dragW/2)))
        return up();
    }
    if(clickRight)
    {
        if(contentL > -(contentW - contentClipW))
        {
            dragL = dragL + (speed*scrollLength);
            if(dragL > (rulerL + scrollW - dragW))
                dragL = (rulerL + scrollW - dragW);
            contentL = contentL - speed;
            if(contentL < -(contentW - contentClipW))
                contentL = -(contentW - contentClipW);
            moveTo();
            timer = setTimeout("scrollForRight()",25);
        }
    }
    return false;
}

function reloadPage()
{
    location.reload();
}

function eventLoader()
{
    if(ie4)
    {
        leftL = document.all.up.style.pixelLeft;
        leftT = document.all.up.style.pixelTop;
        rightL = document.all.down.style.pixelLeft;
        rightT = document.all.down.style.pixelTop;
        dragL = document.all.drag.style.pixelLeft;
        dragT = document.all.drag.style.pixelTop;
        rulerL = document.all.ruler.style.pixelLeft;
        contentW = parseInt(document.all.scrollerContent.scrollWidth);
        contentClipW = parseInt(document.all.scrollerContentClip.style.width);
    }
    else
        if(nn4)
        {
            leftL = document.up.left;
            leftT = document.up.top;
            rightL = document.down.left;
            rightT = document.down.top;
            dragL = document.drag.left;
            dragT = document.drag.top;
            rulerL = document.ruler.left;           
            contentW = document.scrollerContentClip.document.scrollerContent.clip.bottom;       //还未修改
            contentClipW = document.scrollerContentClip.clip.bottom;    //还未修改
        }
        else
            if(dom)
            {
                leftL = parseInt(document.getElementById("up").style.left);
                leftT = parseInt(document.getElementById("up").style.top);
                rightL = parseInt(document.getElementById("down").style.left);
                rightT = parseInt(document.getElementById("down").style.top);
                dragL = parseInt(document.getElementById("drag").style.left);
                dragT = parseInt(document.getElementById("drag").style.top);
                rulerL = parseInt(document.getElementById("ruler").style.left);
                contentW = parseInt(document.getElementById("scrollerContent").offsetWidth);
                contentClipW = parseInt(document.getElementById("scrollerContentClip").offsetWidth);
                document.getElementById("scrollerContent").style.top = 0 + "px";        //这句有什么用?
            }
    scrollLength = ((scrollW-dragW)/(contentW-contentClipW));
    if(nn4)
    {
        document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
        window.onresize = reloadPage;
    }
    document.onmousedown = down;
    document.onmousemove = move;
    document.onmouseup = up;
}

</script>

</head>
<body οnlοad="eventLoader()">
    <span id="drag" style="left: 109px; position: absolute; top: 202px">
        <img height="9" src="slider.gif" width="26" border="1">
    </span>
    <span id="ruler" style="left: 109px">
          <img height="9" src="slider.gif" width="26" border="1">
    </span>
    <span id="up" style="left: 100px;position: absolute; top: 200px">
          <img height="13" alt="" src="scrollup.gif" width="9" border="1">
    </span>
    <span id="down" style="left: 189px; position: absolute; top: 200px">
        <img height="13" alt="" src="scrolldown.gif" width="9" border="1">
    </span>
    <span style="left: 100px; position: absolute; top: 200px">
        <img height="13" alt="" src="scrolldown.gif" width="100" border="1">
    </span>
    <span id="scrollerContentClip" style="left: 100; visibility: visible; overflow:hidden; width: 100; clip: rect(0px 140px 194px 0px);position: absolute; top: 100; height: 100; border: #CC6600 1px solid">
        <span id="scrollerContent" style="width: 200; height: 100">
            <p></p>
            <p></p>
            <p>呵呵呵呵</p>
            <p>在移动焦点到其它元素之后立即触发于当前拥有焦点的元素上触发</p>
            <p>呵呵</p>
            <p>呵呵</p>
            <p>呵呵呵呵</p>
            <p>呵呵呵呵</p>
            <p>呵呵呵</p>
            <p>呵呵呵呵</p>
            <p>呵呵</p>
        </span>
     </span>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值