两个事件处理程序(单击和鼠标移动)同时在一个元素上

时间:2014-04-28 22:27:02

标签: function events click mousemove handlers

我正在创建一种滑块,当单击窗口上的某个点时,该元素会将其宽度拉伸到该点。我还想添加一个拖动事件处理程序,我已经读过mousedown和mousemove以及mouseup是需要组合的三个处理程序。这段代码工作得很好,但我想知道是否有更好的方法来组合这些处理程序,因为目前我正在重复代码,我不确定是否有必要。我是javascript和jquery的新手。谢谢你的帮助

function reposition_bar(mouse_touch_position){
    document.onclick = function(mouse_touch_position){
        var timer_bar_position = $(timer_bar);
        timer_bar_offset = timer_bar_position.offset();
        var total_width_timer = $("#timer_bar_outer0").width();
        var new_width_timer = (((mouse_touch_position.pageX - timer_bar_offset.left)/total_width_timer) * 100); /*converting to percantages because the width of the timer is in percentages in css*/
        player.currentTime = (window.duration)*(new_width_timer/100);
    }
    document.onmousemove = function(mouse_touch_position){
        var timer_bar_position = $(timer_bar);
        timer_bar_offset = timer_bar_position.offset();
        var total_width_timer = $("#timer_bar_outer0").width();
        var new_width_timer = (((mouse_touch_position.pageX - timer_bar_offset.left)/total_width_timer) * 100); /*converting to percantages because the width of the timer is in percentages in css*/
        player.currentTime = (window.duration)*(new_width_timer/100);

    }
    this.onmouseup = function() {
    document.onmousemove = null;
    }

}

这里是我创建事件监听器的地方。

 $(timer_area).on("mousedown", reposition_bar);

我的HTML:

<div id="timer_area0" class="timer_area">
    <div id="timer_bar_outer0" class="timer_bar_outer"></div>
    <div id="timer_bar0" class="timer_bar"></div>
</div>

和我的css:

.timer_area{
position: relative;


width: 100%;
margin: 20px auto 10px auto;
height: 20px;
backgroud: #fff;
}

.timer_bar{  
z-index: -1;
display: block;  
width: 3%;  
height: 8px;  
border-radius: 4px;  
clear: both;  
position: absolute; 
margin-top: 6px;
box-shadow: inset 0px 1px 0px 0px rgba(250,250,250,0.5),  
                0px 0px 3px 2px rgba(250,250,250,1);  
background-color: #fff; 
}  

.timer_bar_outer{
padding: 0px 3% 0px 3%; 
    width: 100%;  /*this should be same number as 100/windowduration*/
    height: 20px;  
    display: block;  
    z-index: -2;  
    position: absolute;  
    background-color: rgb(0,0,0); 
margin-left: -3%;
border-radius: 10px;  
box-shadow: 0px 1px 0px 0px rgba(250,250,250,0.1),   
                inset 0px 1px 2px rgba(0, 0, 0, 0.5); 
box-sizing: content-box;
}

.timer_bar.on{  
   background-color: blue;  
    box-shadow: inset 0px 1px 0px 0px rgba(250,250,250,0.5),  
                0px 0px 3px 2px rgba(5,242,255,1); 
}

@tlindell评论后编辑:

感谢您的评论。我已经尝试了范围滑块但是在做了大量研究之后无法按照我想要的方式设置它们。 (即我的垂直滑块工作完美,直到我在其上放置一个盒子阴影并且它不再垂直工作)...我很满意我的滑块。它只是我想知道我是否可以更好地编写reposition_bar方法因为我上面的方式,这段代码:

document.onclick = function(mouse_touch_position){
            var timer_bar_position = $(timer_bar);
            timer_bar_offset = timer_bar_position.offset();
            var total_width_timer = $("#timer_bar_outer0").width();
            var new_width_timer = (((mouse_touch_position.pageX - timer_bar_offset.left)/total_width_timer) * 100); /*converting to percantages because the width of the timer is in percentages in css*/
            player.currentTime = (window.duration)*(new_width_timer/100);
        }

与exaclty相同,因为它们是不同的事件处理程序,例如onmousemove和onclick:

document.onmousemove = function(mouse_touch_position){
            var timer_bar_position = $(timer_bar);
            timer_bar_offset = timer_bar_position.offset();
            var total_width_timer = $("#timer_bar_outer0").width();
            var new_width_timer = (((mouse_touch_position.pageX - timer_bar_offset.left)/total_width_timer) * 100); /*converting to percantages because the width of the timer is in percentages in css*/
            player.currentTime = (window.duration)*(new_width_timer/100);

        }

所以我可以用一种方式组合它们,例如

document.onmousemove || document.onclick = function(mouse_touch_position){
                var timer_bar_position = $(timer_bar);
                timer_bar_offset = timer_bar_position.offset();
                var total_width_timer = $("#timer_bar_outer0").width();
                var new_width_timer = (((mouse_touch_position.pageX - timer_bar_offset.left)/total_width_timer) * 100); /*converting to percantages because the width of the timer is in percentages in css*/
                player.currentTime = (window.duration)*(new_width_timer/100);

            }

感谢您编辑@tlindell。这是我想要的东西,但我尝试了以下并没有真正具有预期的效果。它没有注册mouseup事件。

function reposition_bar(mouse_touch_position){
    document.onmousemove = function(mouse_touch_position){
        var timer_bar_position = $(timer_bar);
        timer_bar_offset = timer_bar_position.offset();
        var total_width_timer = $("#timer_bar_outer0").width();
        var new_width_timer = (((mouse_touch_position.pageX - timer_bar_offset.left)/total_width_timer) * 100); /*converting to percantages because the width of the timer is in percentages in css*/
        player.currentTime = (window.duration)*(new_width_timer/100);

    }
    this.onmouseup = function() {
    document.onmousemove = null;
    }

}

$(timer_area).on("mousedown click", reposition_bar);

我希望用onmousedown调用该函数,然后在函数中我希望它能处理onmousemove和onclick的事件。我可以在函数reposition_bar中执行类似的操作:

function reposition_bar(mouse_touch_position){
    document.on("mousemove click") = function(mouse_touch_position){
        var timer_bar_position = $(timer_bar);
        timer_bar_offset = timer_bar_position.offset();
        var total_width_timer = $("#timer_bar_outer0").width();
        var new_width_timer = (((mouse_touch_position.pageX - timer_bar_offset.left)/total_width_timer) * 100); /*converting to percantages because the width of the timer is in percentages in css*/
        player.currentTime = (window.duration)*(new_width_timer/100);

    }
    this.onmouseup = function() {
    document.onmousemove = null;
    }

}

$(timer_area).on("mousedown", reposition_bar);

这根本不起作用,所以我想函数中的这一行的语法是错误的:

document.on("mousemove click") = function(mouse_touch_position){

}

再次感谢。我对此很新:)

感谢@tlindell的回答。 这就是我最后所做的。 (参见window.onload部分进行编辑)

<script>
    var player;
    var intv;
    var duration;
    var song_id;
    var button;

    //init
window.onload = function(){
        player = document.getElementById('audio_player');


            $('#volume_control_area').on('mousedown mouseup mouseleave click', function(e){
                if(e.type === 'mousedown'){
                        $(this).bind('mousemove', reposition);
                }
                else if((e.type === 'mouseup') || (e.type === 'mouseleave')) {
                        $(this).unbind('mousemove', reposition);
                }else if(e.type === 'click'){
                        $(this).bind('click', reposition);
                }
            });
}

function reposition(mouse_volume_position){

        var volume_knob_position = $(".volume_control_knob");   
        volume_area_offset = $('#volume_control_area').offset();
        var total_height_volume = $('#volume_control_area').height();
        console.log(total_height_volume);
        var new_height_volume = ((volume_area_offset.top + total_height_volume)- mouse_volume_position.pageY); /*converting to percantages because the width of the timer is in percentages in css*/

        if(new_height_volume > 8 && new_height_volume <= 100){
        console.log(new_height_volume);
        player.volume = new_height_volume/100;
        $(".volume_control_knob").css({

        'height' : new_height_volume + '%' 

        });
        }

}




</script>

1 个答案:

答案 0 :(得分:2)

欢迎Jquery ui!

你可以在这里调整div的大小:

jquery ui resizable

并在此处获取范围滑块:

Jquery ui silders

修改

绝对!这是做到这一点的方法。

$('#element').on('keyup keypress blur change', function() {
    ...
});

或者只是将函数作为参数传递给普通事件函数:

var myFunction = function() {
   ...
}

$('#element')
    .keyup(myFunction)
    .keypress(myFunction)
    .blur(myFunction)
    .change(myFunction)

jQuery multiple events to trigger the same function

更新:

<script>
    let player;
    let intv;
    let duration;
    let song_id;
    let button;

    //init
    window.onload = function(){
        player = document.getElementById('audio_player');

        $('#volume_control_area').on('mousedown mouseup', function(e){
            if(e.type === 'mousedown'){
                $(this).bind('mousemove', reposition);
            }
            else if(e.type === 'mouseup'){
                $(this).unbind('mousemove', reposition);
            }
        })
    }

    function reposition(mouse_volume_position){
        let volume_knob_position = $(".volume_control_knob");   
        let volume_area_offset = $('#volume_control_area').offset();
        let total_height_volume = $(volume_control_area).height();
        let new_height_volume = ((volume_area_offset.top + total_height_volume)- mouse_volume_position.pageY); /*converting to percantages because the width of the timer is in percentages in css*/

        if(new_height_volume > 8 && new_height_volume <= 100){
            console.log(new_height_volume);
            player.volume = new_height_volume/100;
                $(".volume_control_knob").css({

                    'height' : new_height_volume + '%' 

                });
        }
    }   
</script>