使用 jQuery 创建按钮翻转

发布于 2024-10-09 20:15:21 字数 773 浏览 0 评论 0原文

我正在尝试创建一个可以在全局范围内使用的函数来创建图像的翻转状态。基本上,我希望能够向图像添加一个类,然后当您翻转它时,jquery 将使用相同的图像名称和相同的扩展名,但在文件名中添加“-over”。 (我将有一个翻转状态的图像,其名称与非翻转状态相同,除了上面有 -over 。我想出了这个,但它不起作用。我是做错了什么还是有人知道更好的方法吗?

$('.btn').hover(function(){
    $(this).attr("src").split(".jpg").join("Over.jpg"));
});

图像:

<img src="/static/images/overlay-close-button.jpg" alt="Close" title="Close" id="our-staff-overlay-close" class="btn"/>

谢谢!

编辑:有什么方法可以使它不特定于文件时间,它可以找出任何文件类型而不仅仅是 jpg 吗?

编辑

$('.btn').hover(function(){
    this.src = this.src.split(".jpg").join("Over.jpg");
}, function() {
    this.src = this.src.split("Over.jpg").join(".jpg");
 });

它工作得很好

2 :我还可以添加一个活动状态(当单击按钮时)吗?

I'm trying to make a function that I can use globally to create rollover states for images. Basically, I want to be able to add a class to an image, and then when you rollover it, jquery will use the same image name, and same extension, but add "-over" to the file name. (I'll have an image with the rollover state named the same as the non rolled over state except with the -over on it. I came up with this, but it's not working. Am I doing something wrong or does anyone know of a better way to do it?

$('.btn').hover(function(){
    $(this).attr("src").split(".jpg").join("Over.jpg"));
});

image:

<img src="/static/images/overlay-close-button.jpg" alt="Close" title="Close" id="our-staff-overlay-close" class="btn"/>

Thanks!

EDIT: Is there any way to make it non specific to the file time, where it can figure out any file type rather than just jpgs?

I'm using:

$('.btn').hover(function(){
    this.src = this.src.split(".jpg").join("Over.jpg");
}, function() {
    this.src = this.src.split("Over.jpg").join(".jpg");
 });

and it's working great

EDIT 2: Can I also add an active state (when the button is being clicked)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

骄兵必败 2024-10-16 20:15:21

拆分和连接应该按预期工作,您只需将其设置回 img 的 src 属性:

$('.btn').hover(function() {
    var src = $(this).attr("src");
    src = src.split('.jpg').join('-over.jpg');
    $(this).attr("src", src);
});

此外,如果您希望它与任何扩展一起使用,您可以使用如下正则表达式:

$('.btn').hover(function() {
    var src = $(this).attr("src");
    src = src.replace(/(.*)\.(png|gif|jpg|jpeg)$/, "$1-over.$2");
    $(this).attr("src", src);
});

正则表达式匹配任何内容以句点结尾,后跟 png、gif、jpg 或 jpeg 之一,并将其​​替换为第一部分(路径 + 文件名)、字符串“-over”、句点和原来的扩展名。

您可以通过从源代码中删除 -over 将其替换回原始状态:

$('.btn').hover(function() {
    var src = $(this).attr("src");
    src = src.replace(/(.*)\.(png|gif|jpg|jpeg)$/, "$1-over.$2");
    $(this).attr("src", src);
}, function() {
    var src = $(this).attr("src");
    src = src.replace(/(.*)-over\.(png|gif|jpg|jpeg)$/, "$1.$2");
    $(this).attr("src", src);
});

jQuery().hover 事件接受两个函数,第一个函数在您启动悬停时调用,第二个函数在您开始悬停时调用。当您退出悬停时调用。

The splitting and joining should work as intended, you just need to set that back to the src attribute of the img:

$('.btn').hover(function() {
    var src = $(this).attr("src");
    src = src.split('.jpg').join('-over.jpg');
    $(this).attr("src", src);
});

Also, if you want it to work with any extension, you could use a regular expression like this:

$('.btn').hover(function() {
    var src = $(this).attr("src");
    src = src.replace(/(.*)\.(png|gif|jpg|jpeg)$/, "$1-over.$2");
    $(this).attr("src", src);
});

The regular expression matches anything that ends with a period followed by one of png, gif, jpg, or jpeg, and replaces it with the first part (the path + filename), the string "-over", a period, and the original extension.

You can replace it back to the original state by removing the -over from the source:

$('.btn').hover(function() {
    var src = $(this).attr("src");
    src = src.replace(/(.*)\.(png|gif|jpg|jpeg)$/, "$1-over.$2");
    $(this).attr("src", src);
}, function() {
    var src = $(this).attr("src");
    src = src.replace(/(.*)-over\.(png|gif|jpg|jpeg)$/, "$1.$2");
    $(this).attr("src", src);
});

The jQuery().hover event accepts two functions, the first one is called when you start the hover, the second one is called when you exit the hover.

海螺姑娘 2024-10-16 20:15:21

你只是没有对结果做任何事情。在这种情况下,您想要设置src(可以通过几种方式完成),这是最有效的示例(无需更改您的.split())。 join() 方法):

$('.btn').hover(function(){
  this.src = this.src.split(".jpg").join("Over.jpg");
}, function() {
  this.src = this.src.split("Over.jpg").join(".jpg");
});

You're just not doing anything with the result. In this case you want to set the src (which can be done a few ways), here's the most efficient example (without changing your .split().join() method):

$('.btn').hover(function(){
  this.src = this.src.split(".jpg").join("Over.jpg");
}, function() {
  this.src = this.src.split("Over.jpg").join(".jpg");
});
淡淡離愁欲言轉身 2024-10-16 20:15:21
$('.btn').hover(function(){
    $(this).attr("src", this.src.split(".jpg")[0] + "-over.jpg");
}, function() {
   $(this).attr("src", this.src.split("-over.jpg")[0] + ".jpg");
})
$('.btn').hover(function(){
    $(this).attr("src", this.src.split(".jpg")[0] + "-over.jpg");
}, function() {
   $(this).attr("src", this.src.split("-over.jpg")[0] + ".jpg");
})
日裸衫吸 2024-10-16 20:15:21

您可能需要考虑使用纯 CSS 来实现翻转效果。确实没有必要在像图像翻转这样琐碎的事情中涉及脚本。另外,对于那些在关闭脚本的情况下浏览的偏执狂来说,纯 CSS 方法仍然有效。

使用 :hover:active 伪类并利用 CSS 精灵

这里的缺点是这种方法很难普遍适用,因为您需要事先知道图像的大小。

You may want to consider achieving your rollover effect using pure CSS. It's really not necessary to involve script in something as trivial as a image rollover. Plus, a pure CSS approach will still work for the paranoids out there who browse with script turned off.

Use the :hover and :active pseudoclasses and take advantage of CSS sprites.

The downside here is that this method is harder to make generally applicable because you need to know the size of your images before-hand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文