使用HTML扩展图像&Jquery
Image expansion with HTML & Jquery
我看到了这段代码,并试图模仿它,但它在我的电脑或浏览器上不起作用
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<script>
var current_h = null;
var current_w = null;
$('.resize').hover(
function(){
current_h = $(this, 'img')[0].height;
current_w = $(this, 'img')[0].width;
$(this).stop(true, false).animate({width: (current_w * 1.3), height: (current_h * 1.3)}, 300);
},
function(){
$(this).stop(true, false).animate({width: current_w + 'px', height: current_h + 'px'}, 300);
}
);
</script>
<div class="box">
<img src="http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png" class="resize" width="250"/>
</div>
</body>
</html>
谁能告诉我我哪里做错了?
把它放在你的CSS链接下面,在你的<head>
标签里面。
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
你想让它在什么浏览器中工作?
更新:
用:
$(document).ready(function() {
// Code here...
});
如果你看一下fiddle的代码,整个函数都在"load"事件中。试试那个或ready函数
$(window).load(function(){
var current_h = null;
var current_w = null;
$('.resize').hover(
function(){
current_h = $(this, 'img')[0].height;
current_w = $(this, 'img')[0].width;
$(this).stop(true, false).animate({width: (current_w * 1.3), height: (current_h * 1.3)}, 300);
},
function(){
$(this).stop(true, false).animate({width: current_w + 'px', height: current_h + 'px'}, 300);
}
);
});
将这段代码包装在document.ready
中并不是一个坏主意:
$(document).ready(function() {
$('.resize').hover(
function() {
current_h = $(this, 'img')[0].height;
current_w = $(this, 'img')[0].width;
$(this).stop(true, false).animate({width: (current_w * 1.3), height: (current_h * 1.3)}, 300);
},
function() {
$(this).stop(true, false).animate({width: current_w + 'px', height: current_h + 'px'}, 300);
}
);
});
将$(document).ready
添加到javascript代码中并更新您的代码。这是新的
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<script>
var current_h = null;
var current_w = null;
$(document).ready(function () {
$('.resize').hover(
function () {
current_h = $(this, 'img')[0].height;
current_w = $(this, 'img')[0].width;
$(this).stop(true, false).animate({
width: (current_w * 1.3),
height: (current_h * 1.3)
}, 300);
},
function () {
$(this).stop(true, false).animate({
width: current_w + 'px',
height: current_h + 'px'
}, 300);
});
});
</script>
<div class="box">
<img src="http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png" class="resize" width="250"/>
</div>
</body>
</html>
相关文章:
- jQuery图像幻灯片,在停止后淡出到第一张图像
- 使用next、prev按钮制作简单的jQuery图像滑块
- Jquery图像循环问题
- 为什么这个jQuery图像映射在WordPress上不起作用
- JQuery 图像错误处理
- jQuery 图像替换为类
- jquery 图像旋转器(仅在 JS 中传递图像)
- jQuery图像在悬停时闪烁
- 使用 nameProp 的 JQuery 图像滚动更新在 Firefox 和 Chrome 中不起作用
- JQuery 图像过渡.向上滑动
- jQuery图像淡入淡出不起作用
- JQuery图像预览无法工作
- JQuery图像滑块和CSS转换只能工作一次
- jQuery图像(产品)滑块
- jQuery-图像替换转换〔Safari中的问题〕
- 使jQuery图像滑块停止在最后一个图像,并添加下一个和上一个按钮
- jquery图像滑块库
- JQuery图像大小计算没有'不能在Chrome/Safari中工作
- 如何使JQuery图像滑块与屏幕的宽度相等
- wordpress插件中的jQuery图像幻灯片