2017-01-13 34 views
0

我已經搜索了很多,但是我找不到任何解決方案,或者可能是我無法找到問題的好關鍵字。 我正在使用音頻播放器,問題在於音量控制。 這裏是我的代碼拖拽功能應該只當鼠標按下功能工作,否則它不應該發生的工作,但它並不在離開鼠標按鈕禁用的OnMouseMove事件,所以我需要你的幫助..在jquery中由mousedown解僱mouseup時發生的事件

$(document).ready(function(){ 
    $(".volume_wrapper_slider").on("mousedown", function(e) { 
     $(this).on("mousemove", function(e) { 
      isDragging = true; 
      var slider_width = $(this).width(); 
        var slider_offset = $(this).offset().left; 
        var percentage1 = (100/slider_width); 
        var current_percentage = percentage1 * (e.clientX-slider_offset);   
         // move the bar w.r.t click 
        $(".vol_slided").width(current_percentage+"%"); 
        now_playing.volume = parseFloat(percentage1 * ($(".vol_slided").width()/100)); 
     }); 
    }); 
      // mouse up 
    $(".volume_wrapper_slider").mouseup(function(e) { 
     e.stopPropagation(); 
    }); 
}); 

更新 - ----------------------------

這解決了我的問題,感謝@ jaromanda-X

 // volume control 

$(document).ready(function(){ 
     $(".volume_wrapper_slider").on("mousedown", function(e) { 
      canDrag = true; 
      $(this).on("mousemove", function(e) { 
       if (canDrag == true) { 
       var slider_width = $(this).width(); 
         var slider_offset = $(this).offset().left; 
         var percentage1 = (100/slider_width); 
         var current_percentage = percentage1 * (e.clientX-slider_offset); 

          // move the bar w.r.t click 
         $(".vol_slided").width(current_percentage+"%"); 
         now_playing.volume = parseFloat(percentage1 * ($(".vol_slided").width()/100)); 
       } 

      }); 
       // mouse up 
     $(".volume_wrapper_slider").mouseup(function(e) { 
      canDrag = false; 
     }); 
    }); 
}); 
+0

你試圖阻止什麼事件?你有嘗試使用'.off()'嗎? –

+0

你想設置isDragging真正的mousedown和isDragging錯誤的mouseup和只處理移動事件,如果isDragging是真的......基本上,可能會更多,你需要擔心,如果鼠標被釋放的問題以外的元素 –

+0

謝謝@JaromandaX,讓我試試 –

回答

0

那爲我工作..

 // volume control 

$(document).ready(function(){ 
     $(".volume_wrapper_slider").on("mousedown", function(e) { 
      canDrag = true; 
      $(this).on("mousemove", function(e) { 
       if (canDrag == true) { 
       var slider_width = $(this).width(); 
         var slider_offset = $(this).offset().left; 
         var percentage1 = (100/slider_width); 
         var current_percentage = percentage1 * (e.clientX-slider_offset); 

          // move the bar w.r.t click 
         $(".vol_slided").width(current_percentage+"%"); 
         now_playing.volume = parseFloat(percentage1 * ($(".vol_slided").width()/100)); 
       } 

      }); 
       // mouse up 
     $(".volume_wrapper_slider").mouseup(function(e) { 
      canDrag = false; 
     }); 
    }); 
}); 
相關問題