簡體   English   中英

使用jQuery放大鼠標懸停時的圖像

[英]Using jquery to enlarge image on mouseover/hover

演示這是我到目前為止的代碼

我想在每個鼠標懸停或使用jquery懸停時分別放大每個圖像。 我有我認為的代碼主干,但是尚不能完全正常工作。 我是jQuery的一個新手,因此嘗試在線閱讀一些文章非常具有挑戰性,而在花了一個小時研究這個問題之后,我已經接近了自己。

HTML

<img src="http://www.j-creative.biz/wp-content/uploads/2015/10/primary-colours-300x171.png" id="test" alt="" />
<br>
<img src="http://www.j-creative.biz/wp-content/uploads/2015/10/primary-colours-300x171.png" id="test1" alt="" />

jQuery的

$('#test').hover(function() {
$(this).css({ width: "100%", height: "100%" });
 }, function () {
        $(this).css({ width: auto, height: auto });
});

請嘗試這個

$(document).on({
    mouseenter: function () {
        $(this).css({ width: "100%", height: "100%" });
    },

    mouseleave: function () {
        $(this).css({ width: "auto", height: "auto" });
    }
}, '#test');

小提琴演示

您的答案缺少$(this).css({ width: "auto", height: "auto" });

$('#test').hover(function() {
$(this).css({ width: "100%", height: "100%" });
 }, function () {
        $(this).css({ width: "auto", height: "auto" });
});

演示答案

$('#test').hover(function() {
  $(this).css({ width: "100%", height: "100%" });
}).mouseleave( function () {
  $(this).css({ width: "auto", height: "auto" });
});

並且您需要在jsfiddle中設置jquery https://jsfiddle.net/1mnLwgny/

1. auto不是變量。 放在逗號中間

2.添加JQuery庫

工作演示

$('#test').hover(function() {
$(this).css({ width: "100%", height: "100%" });
 }, function () {
        $(this).css({ width: "auto", height: "auto" });
});

不要忘記在小提琴中包含JQuery。

$('#test').hover(function() {
$(this).css({ width: "100%", height: "100%" });
 }, function () {
        $(this).css({ width: "auto", height: "auto"});
});

工作版本: 小提琴

這將同時作用於兩個圖像並將其分別放大

$('img').on('mouseover', function() {
    $(this).css({ width: "100%", height: "100%" });
});

還有一件事,您的DEMO不包含jQuery。 在此處輸入圖片說明

你快到了 只是您忘記了包含JQuery庫。

包含<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

並將JQuery更改為以下內容:

$('img').hover(function() {
$(this).animate({ width: "100%", height: "100%" });
 }).mouseleave( function () {
        $(this).animate({ width: "auto", height: "auto" });
});

工作小提琴

暫無
暫無

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

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