當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


JQuery mousemove()用法及代碼示例


jQuerymousemove()method 是一個內置方法,當鼠標指針移動到所選元素上時使用。

用法:

$(selector).mousemove(function)

參數:該方法接受單個參數函數這是可選的。該參數用於指定調用 mousemove 事件時要運行的函數。

下麵的示例說明了 jQuery 中的 mousemove() 方法:

示例 1:此示例說明了mousemove()方法的使用。

HTML


<!DOCTYPE html> 
<html> 
  
<head> 
    <title>The mousemove Method</title> 
    <script src= 
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
    </script> 
  
    <!-- jQuery code to show the working of this method -->
    <script> 
        $(document).ready(function () { 
            $("p").mousemove(function () { 
                $("div").css("background-color", "lightgreen"); 
            }); 
        }); 
    </script> 
    <style> 
        div { 
            width: 380px; 
            padding: 20px; 
            height: 60px; 
            border: 2px solid green; 
            font-weight: bold; 
            font-size: 20px; 
        } 
    </style> 
</head> 
  
<body> 
    <div> 
        <!-- move over this text to see the change -->
        <p>Move over this paragraph.</p> 
    </div> 
</body> 
  
</html>

輸出:

示例2:在這個例子中,當我們將鼠標移到段落上時,會出現一個彈出窗口。

HTML


<!DOCTYPE html> 
<html> 
  
<head> 
    <title>The mousemove Method</title> 
    <script src= 
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
    </script> 
  
    <!-- jQuery code to show the working of this method -->
    <script> 
        $(document).ready(function () { 
            $("p").mousemove(function () { 
                alert("Mouse moved over this paragraph"); 
            }); 
        }); 
    </script> 
    <style> 
        div { 
            width: 380px; 
            padding: 20px; 
            height: 60px; 
            border: 2px solid green; 
            font-weight: bold; 
            font-size: 20px; 
        } 
    </style> 
</head> 
  
<body> 
    <div> 
        <!-- move over this text and pop-up will come out -->
        <p>Move over this paragraph.</p> 
    </div> 
</body> 
  
</html>

輸出:



相關用法


注:本文由純淨天空篩選整理自kundankumarjha大神的英文原創作品 jQuery mousemove() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。