无法添加两个String(+ =操作)

时间:2015-07-01 18:24:22

标签: javascript

我正在开发一个项目来获取用户鼠标滚轮移动并知道它向上或向下滚动。 在我的代码中,我可以上下移动。 但我想将动作保存为字符串。 例如,如果用户向上滚动向上 'mhmh'显示返回“UUD”,但现在它只是最后一次移动(只是U o D)

<script type="text/javascript">

function displaywheel(e){
    var evt=window.event || e //equalize event object
    var delta=evt.detail? evt.detail*(-120) : evt.wheelDelta
    //check for detail first so Opera uses that instead of wheelDelta
    var mhmh = ''
    var t = ''
    //mhmh = (delta<=-120)? mhmh + 'D' : mhmh + 'U'
    if (delta<=-120)
        t = "d"
    else
        t = "u"

    mhmh = mhmh + t
    document.getElementById("wheelvalue").innerHTML= mhmh //delta returns +120 when wheel is scrolled up, -120 when down
}

var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x

if (document.attachEvent) //if IE (and Opera depending on usesr setting)
    document.attachEvent("on"+mousewheelevt, displaywheel)
else if (document.addEventListener) //WC3 browsers
    document.addEventListener(mousewheelevt, displaywheel, false)
</script>

你可以sample web page here。 问题是什么?

3 个答案:

答案 0 :(得分:2)

问题是每次调用事件处理程序时都要重新定义mhmh。这是closures派上用场的地方。将您的mhmh变量存储在事件处理程序上下文之外:

var mhmh = '';

function displaywheel(e){
    var evt=window.event || e //equalize event object
    var delta=evt.detail? evt.detail*(-120) : evt.wheelDelta
    //check for detail first so Opera uses that instead of wheelDelta

    var t = ''
    //mhmh = (delta<=-120)? mhmh + 'D' : mhmh + 'U'
    if (delta<=-120)
        t = "d"
    else
        t = "u"

    mhmh = mhmh + t
    document.getElementById("wheelvalue").innerHTML= mhmh //delta returns +120 when wheel is scrolled up, -120 when down
}

var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x

if (document.attachEvent) //if IE (and Opera depending on usesr setting)
    document.attachEvent("on"+mousewheelevt, displaywheel)
else if (document.addEventListener) //WC3 browsers
    document.addEventListener(mousewheelevt, displaywheel, false)

答案 1 :(得分:0)

你的变量&#39; mhmh&#39;在函数的本地范围内,它总是被重置。在函数外部使其全局化,然后可以连接鼠标移动并在需要时重置变量。

答案 2 :(得分:0)

只需将变量mhmh作为全局变量

`

var mhmh = '';
function displaywheel(e){
    var evt=window.event || e //equalize event object
    var delta=evt.detail? evt.detail*(-120) : evt.wheelDelta
    //check for detail first so Opera uses that instead of wheelDelta

    var t = ''
    //mhmh = (delta<=-120)? mhmh + 'D' : mhmh + 'U'
    if (delta<=-120)
        t = "d"
    else
        t = "u"

    mhmh = mhmh + t
    document.getElementById("wheelvalue").innerHTML= mhmh //delta returns +120 when wheel is scrolled up, -120 when down
}

var mousewheelevt=(/Firefox/i.test(navigator.userAgent))? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x

if (document.attachEvent) //if IE (and Opera depending on usesr setting)
    document.attachEvent("on"+mousewheelevt, displaywheel)
else if (document.addEventListener) //WC3 browsers
    document.addEventListener(mousewheelevt, displaywheel, false)

`