JavaScript--鼠標滾動改變圖片大小


鼠標滾動改變圖片的大小:

原理:當鼠標滾動時改變了zoom的值;

<!DOCTYPE HTML>
<html>
<head>
<title>通過鼠標滾輪放大縮小圖片</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
div {
            
            height: 200px;
            background: pink;
            text-align: center;
        }
        img {
             margin: 0 auto;         
        }

</style>
</head>
<script language="javascript">
function bigimg(obj) {
    //obj是一個對象,初始時obj並沒有zoom屬性,所以給zoom賦值為100;
    var zoom = parseInt(obj.style.zoom)||100;
    //每次滾動鼠標時,改變zoom的大小
    //event.wheelDelta有兩個值,120,-120,取值情況取決於滾動鼠標的方向;
    zoom += event.wheelDelta/12;//每次滾動加減10;
    if (zoom > 0) {
        obj.style.zoom = zoom+"%";//更改后的zoom賦值給obj
        console.log(obj.style.zoom);
    }
    return false;
    
}
</script>
<body>
<div>
    <img src = "images/6.jpg" width="500px" heigth="450px" onmousewheel="bigimg(this)">
</div>
</body>
</html>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM