[英]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"});
});
工作版本: 小提琴
你快到了 只是您忘記了包含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.