gpt4 book ai didi

javascript - 创建自定义 jQuery 类

转载 作者:行者123 更新时间:2023-11-29 22:24:11 25 4
gpt4 key购买 nike

我正在为我的网站创建一个图片库。我想要在页面加载时显示照片的网格。然后,当用户将鼠标悬停在每张照片上时,照片会放大一点。

我为悬停创建了 javascript,但我不知道如何将它正确地打包到一个类中。

基本上,我只想创建一个这样的列表

<ul>
<li><img src="img1.jpg" /></li>
<li><img src="img2.jpg" /></li>
</ul>

然后它会自动创建每个图像,我的悬停机制已经到位。到目前为止,这是我的代码。

<!DOCTYPE HTML>
<html>
<head>
<script src="jquery.js"></script>
<style text="text/css">
.hoverImage {
position: absolute;
width: 200px;
left: 500px;
top: 200px;
}
</style>
</head>
<body>
<script>
var originalWidth;
var originalHeight;

function onHover() {
originalWidth = $(this).width();
originalHeight = $(this).height();

$(this).stop(false, true).animate({
left: $(this).offset().left-(Math.floor(originalWidth/4)),
top: $(this).offset().top-(Math.floor(originalHeight/4)),
width: 300,
},200);
}

function offHover() {
$(this).stop(false, true).animate({
left: $(this).offset().left+(Math.floor(originalWidth/4)),
top: $(this).offset().top+(Math.floor(originalHeight/4)),
width: 200,
},200);
}

$(document).ready(function() {
$("img").hover(onHover, offHover);
});

</script>
<img class="hoverImage" src="Test.jpg"/>
</body>
</html>

最佳答案

如果你想用你自己的方法扩展 jQuery DOM 对象,这应该是实现它的方法

$.fn.extend({
relyntsHoverPlugin : function() {}
});

这将允许像这样的语法

$('ul').relyntsHoverPlugin();

不要与使用新函数扩展 jQuery 对象混淆,即。 $.relyntsMethod();

希望这对您有所帮助并且我没有完全偏离基地!

关于javascript - 创建自定义 jQuery 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10594706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com