簡體   English   中英

在jQuery拖動事件中檢測懸停

[英]Detect hover in jquery drag event

我正在使用Jquery UI drag/drop效果,並且在拖動元素時,我需要執行一段代碼來檢測元素是否hovered

更新:實時示例: 這里

這是我的拖動代碼:

<script type="text/javascript">
   $(function(){
      $('.list-disc').sortable({
         connectWith: '.list-disc',

         drag: function(event, ui){
            $(".prof-name").hover(function(event) {
               var $obj = event.target;
               if (!timeoutId) {
                  timeoutId = window.setTimeout(function() {
                  timeoutId = null;
                  $($obj).next().slideToggle();
                  }, 2000);
               }
            },
            function (event) {
            if (timeoutId) {
               window.clearTimeout(timeoutId);
               timeoutId = null;
            }
            else {
               $(event.target).next().slideToggle();
            }
         });
         },

        start: function (event, ui){
           //SomeCode
        },
        stop: function (event, ui){
          //SomeCode
        }
        })
</script>    

該文件說:

拖動(事件,用戶界面)
在拖動過程中鼠標移動時觸發,緊接當前移動發生之前。

也許我理解錯了或者代碼出了點問題...
如您所見,我可以在$(document).ready(function(){}執行該代碼時,在drag::事件中運行該代碼,但是我需要在拖動元素時進行測試,測試是否拖動了元素將鼠標懸停在該對象上2秒鍾,然后我將slideToggle()

還嘗試編寫一個簡單的console.log()來驗證是否正在觸發,但是什么也沒有...

*更新:*嘗試羅恩的答案:(仍然不起作用-什么也沒有發生)

over: function(event, ui){
   var timeoutId;
   $(".prof-name").hover(function(event) {
      var $obj = event.target;
      if (!timeoutId) {
         timeoutId = window.setTimeout(function() {
            timeoutId = null;
            $(this).next().slideToggle();
         }, 2000);
      }
   },
   function (event) {
   if (timeoutId) {
      window.clearTimeout(timeoutId);
      timeoutId = null;
   }
   else {
      $(event.target).next().slideToggle();
   }
   });
}

嘗試這個:

$( ".list-disc").sortable({
  over: function( event, ui ) { // $(this) will be the current hovered element }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM