悬停淡入淡出
我正在尝试创建一个悬停在动作上,该动作会带来彩色图像,而且一旦悬停被删除,它就会淡出回原来的图像。
目前,它会将图像淡出为空,然后淡入新图像。无论我是否将鼠标悬停,它都将保持在原位。
//Loop through the images and print them to the page
for (var i=0; i < totalBoxes; i++){
$.ajax({
url: "random.php?no=",
cache: false,
success: function(html) {
// following line I originally suggested, but let's make it better...
//$('#bg').append(html).fadeIn('slow');
// also note the fine difference between append and appendTo.
var $d = $(html).hide().appendTo('#bg').fadeIn('slow');
$('img', $d).hover(function() {
var largePath = $(this).attr("rel");
$(this).fadeOut("slow", function() {
$(this).attr({ src: largePath }).fadeIn("slow");
});
});
}
});
}
更新:
请查看此链接:
我正在尝试让图像在翻转时交叉淡入淡出,并在淡出时淡出...
任何人都可以帮忙吗,我非常接近,我花了几个小时试图解决这个问题...
I am trying to create a hover over action which brings in a coloured image and also once the hover is removed it fades back to its original image.
Currently it fades out the image to nothing and then fades the new one in. This will then stay in place regardless of whether i hover off or no.
//Loop through the images and print them to the page
for (var i=0; i < totalBoxes; i++){
$.ajax({
url: "random.php?no=",
cache: false,
success: function(html) {
// following line I originally suggested, but let's make it better...
//$('#bg').append(html).fadeIn('slow');
// also note the fine difference between append and appendTo.
var $d = $(html).hide().appendTo('#bg').fadeIn('slow');
$('img', $d).hover(function() {
var largePath = $(this).attr("rel");
$(this).fadeOut("slow", function() {
$(this).attr({ src: largePath }).fadeIn("slow");
});
});
}
});
}
Update:
Please take a look at this link:
I am trying to get the images to cross fadein on rollover and out on fadeout...
Can anyone help, im so close and i have spent hours and hours trying to get my head around this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
hover()
采用两个函数参数,一个用于mouseover
,另一个用于mouseout
。您只提供了第一个。您需要提供第二个来撤消鼠标移开时的图像交换。如果您希望
fadeOut
和fadeIn
并发,请勿将fadeIn
放在fadeOut
的回调中>。只需将它们设为单独的语句即可:按照您的方式,
fadeIn
在fadeOut
完成之前不会开始。这样,它们就会同时开始和结束。hover()
takes two function parameters, one formouseover
and the other formouseout
. You've only supplied the first. You'll need to supply the second to undo the image swapping onmouseout
.If you want the
fadeOut
andfadeIn
to be concurrent, don't put thefadeIn
in the callback of thefadeOut
. Just make them separate statements:The way you have it, the
fadeIn
doesn't start until thefadeOut
is done. This way, they'll both start and finish at the same time.我认为您需要存储原始图像路径(这是您想要在悬停时淡出的路径,对吧),然后在悬停时恢复它。
假设使用“明亮”图像作为 src 属性,并且您使用不透明度来实现效果。
I think you need to store the original image path (which is what you want to fade back to on hover out, right), then restore it on the hover out.
Assuming that the "bright" image is used as the src attribute and you use opacity to achieve the effect.
我可以建议: http://colorpowered.com/blend/
它会做你想做的事情。
编辑:
好吧,首先,我肯定会更改代码的 ajax 部分,让它通过 json 返回所有图像(更好的是我会在后端执行此操作,但是,我不确定您的网站如何设置)。不管怎样,你似乎不必要地淡出了其他图像。只需将彩色图像放置在其他图像上方并进行绝对定位即可。也许你的代码看起来像这样:
Javascript:
PHP:
这基本上应该可以工作。那里可能有一些错别字之类的东西,但据我所知,它应该可以工作。当然,根据您的情况,您可能需要调整/更改其中的一些内容。
祝你的项目好运!
重要编辑:我忘记在 PHP 代码中添加关键部分。我将“rel”属性添加到
标记中。
Might I suggest: http://colorpowered.com/blend/
It will do what you are looking to do.
Edit:
Okay, well, for one, I would definitely change the ajax part of your code to have it return all your images via json (even better I would do it on the back-end, but, I'm not sure how your site is setup). Anyways, it seems like you are fading out your other image unnecessarily. Simply place the color image above the other image with absolute positioning. Maybe your code could look something like this:
Javascript:
PHP:
That should basically work. There might be some typos and stuff in there but, from what I can tell, it should work. Of course, depending on your scenario, you may need to tweak/alter some of this.
Good luck on your project!
IMPORTANT EDIT: I forgot to add a key part to the PHP code. I added the "rel" attrs to the
<img>
tags.您可以使用一些额外的代码来完成此操作。将淡入图像放置在淡出图像的顶部,不透明度设置为 0。将悬停代码添加到淡入图像(它位于顶部,因此可以获取事件)。
两个图像都会淡入和淡出,并且使用 stop() 函数,鼠标快速进出不会导致一系列重复的动画。
You could do this using some additional code. Position the fade-in image on top of the fade-out image, with opacity set to 0. Add the hover code to the fade-in image (it's on top, so it gets the events).
Both images fade in and out, and with the use of the stop() function, mousing in and out rapidly won't lead to a series of repeated animations.
如果要进行交叉淡入淡出,则需要两张图像,一张正在淡入,另一张同时淡出。看一下我制作的具有此效果的页面。我写的插件就在页面源代码中,所以你可以看一下。两个图像需要具有
position:absolute;
以便它们交叉淡入淡出时可以占据页面的相同区域。正如 No Surprises 所说,您只为鼠标悬停提供对
hover
的回调,而不是为鼠标取消悬停提供回调,后者是您将交叉淡入淡出回原始状态的回调。这段代码可能会起作用,记住 CSS 中的绝对定位,并且您可能想要向 backImg 添加 CSS 类,并且图像必须位于订阅悬停事件的离散父元素中:
If you want to do a cross-fade you need two images, one that is fading in, and one that is fading out concurrently. Take a look at a page I did with this effect. The plugin I wrote is right in the page source so you can take a look at it. The two images will need to have
position: absolute;
so that as they cross-fade they can occupy the same area of the page.And like No Surprises said, you are only supplying a callback to
hover
for the mouse hover, and not the mouse un-hover which is where you would cross-fade back to the original.This code may work, remember the absolute positioning in your CSS, and you may want to add a CSS class to the backImg, and the images must be in a discrete parent element that the hover event is subscribed on:
我在这里发现了一个问题...
这不是一个有效的选择器。请尝试以下操作:
您不想使用 <>在您的选择器中,属性选择器语法应如下所示: [attr="value"]
您应该注意,我在代码中颠倒了 ' 和 " 的使用 - 这就是我的做法,它在语法上是相同的。您选择的引号没有任何问题。
I spotted a problem here...
This isn't a valid selector. Try this instead:
You don't want to use the <> in your selector, and the attribute selector syntax should look like this: [attr="value"]
You should note that I reversed the use of ' and " in my code - that's just how I do it, it's syntactically identical. There's nothing wrong with your choice of quotes.
好的,谢谢大家的帮助...我得到了某个地方...我不太高兴,因为它比我最初预期的要慢,因为我现在加载两个图像而不是一个...使用 rel 属性使它变得很多更快,因为我只在悬停时加载图像...
但这里有一个解决方案,谢谢大家...
和我的 php 打印...
OK thanks to you all for your help...i got somewhere...i am not totally happy as its slower than i originally intended because im loading two images in now as apposed to one...using the rel attribute made it alot quicker becaue i was loading the image only on hover...
But here is a solution thanks to you all...
and my php prints...
在你的样式表中添加:
然后让你的成功函数读取:
UPDATE
要解决加载缓慢的问题,你需要让你的 PHP 返回一个包含所有图像的对象,如下所示(假设它名为 images.php -- 将下面的代码放入
) 中(你想使用 json_encode() 但他使用的是较旧版本的 PHP):
然后在 javascript 中你想要:
这段代码应该可以工作...但我还没有测试过。可能有错别字。但这就是它的要点。
In your stylesheet add:
Then make your success function read:
UPDATE
To solve the slow loading problem you'll need to have your PHP return an object of all images like so (let's say it's called images.php -- put the code below inside of
<?php ?>
) (you'd want to use json_encode() but he was on an older version of PHP):Then in javascript you want:
This code should work...but I haven't tested it. There might be typos. But this is the gist of it.