欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

鼠标滚轴控制文本框值的JS代码

时间:2021-09-29 08:48:16|栏目:JavaScript代码|点击:

复制代码 代码如下:

<html>
<head>
<title></title>
<script type="text/javascript" language="javascript">

    /*根据获得焦点的时候给setVal给值*/
    var setVal;  
    var bool = false;    
    var scrollFunc=function(e){ 
        var direct=0; 
        e=e || window.event; 
        var ew = e.wheelDelta
        if(ew && bool){
            if(ew > 0){
                var num1 = setVal.value;
                if(num1 > 0){
                    num1 = eval(1+parseInt(num1));
                }else{
                    num1 = 1;
                }
            }else{
                var num1 = setVal.value;
                if(num1 > 0){
                    num1 = eval(parseInt(num1)-1);
                }else if(num1 == 0){
                    num1 = 0;
                }else{
                    num1 = 1;
                }   
            }

            setVal.value = num1;
        }

    } 
    window.onmousewheel=document.onmousewheel=scrollFunc;

    function ufocus(eve){
        bool = true;
        setVal = eve;
    }

    function ublur(){
        bool = false;
        setVal = null;
    }
</script>
</head>

<body>
<input type="text" onfocus="ufocus(this)" onblur="ublur(this)"/>
</body>

</html>

<html>

<head>

<title></title>
<script type="text/javascript" language="javascript">

    /*根据获得焦点的时候给setVal给值*/
    var setVal;
    var bool = false;  
    var scrollFunc=function(e){
        var direct=0;
        e=e || window.event;
        var ew = e.wheelDelta
        if(ew && bool){
            if(ew > 0){
                var num1 = setVal.value;
                if(num1 > 0){
                    num1 = eval(1+parseInt(num1));
                }else{
                    num1 = 1;
                }
            }else{
                var num1 = setVal.value;
                if(num1 > 0){
                    num1 = eval(parseInt(num1)-1);
                }else if(num1 == 0){
                    num1 = 0;
                }else{
                    num1 = 1;
                } 
            }

            setVal.value = num1;
        }

    }

    window.onmousewheel=document.onmousewheel=scrollFunc;

    function ufocus(eve){
        bool = true;
        setVal = eve;
    }

    function ublur(){
        bool = false;
        setVal = null;
    }
</script>
</head>

<body>
<input type="text" onfocus="ufocus(this)" onblur="ublur(this)"/>
</body>

</html>

上一篇:JS实现图片预览的两种方式

栏    目:JavaScript代码

下一篇:DIV菜单层实现代码

本文标题:鼠标滚轴控制文本框值的JS代码

本文地址:http://www.codeinn.net/misctech/177965.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有