简体   繁体   English

jQuery:更改复选框上的图像单击

[英]jQuery: Change image on checkbox click

I want to change an image when a checkbox is clicked. 我想在单击复选框时更改图像。 Until the new image is fully loaded a loader image should appear on top of the current image. 在新映像完全加载之前,加载程序映像应出现在当前映像的顶部。 While loading the opacity of the current image should also change to 0.5. 加载时,当前图像的不透明度也应更改为0.5。

The function should therefore consist of the following steps: 因此,该功能应包括以下步骤:

On checkbox click: 在复选框上,单击:

  • Change img src 更改img src

While loading: 加载时:

  • Set opacity of current image to 0.5 将当前图像的不透明度设置为0.5
  • Display loader.gif 显示loader.gif

When new image is loaded: 加载新图像时:

  • Change opacity back to 1.0 将不透明度更改回1.0
  • Hide loader.gif 隐藏loader.gif
  • Display new image 显示新图片

How can this be done with jQuery? jQuery如何做到这一点?

Thanks in advance for all proposals! 预先感谢所有建议!

The steps should actually be 这些步骤实际上应该是

  1. on checkbox click, start to preload the new image 在复选框上单击,开始预加载新图像
    Start by creating an Image object, and then setting its load property to a function that will be called once the image has been completely loaded. 首先创建一个Image对象,然后将其load属性设置为一个函数,一旦图像完全加载,该函数将被调用。 Then ( after setting the load property ) set the src attribute of the Image object we created to the Url of your image. 然后( 在设置load属性之后 )将我们创建的Image对象的src属性设置为图像的Url。

  2. while waiting, set the opacity, show the loader 等待时,设置不透明度,显示加载程序
    Opacity you can control with the css property opacity . 您可以使用css属性opacity控制opacity You should have the loader already in the page but hidden, and just show it while the preloading is in progress.. 您应该已经在页面中安装了加载器,但是已经隐藏了,只需在进行预加载时显示它即可。

  3. when preload is complete hide preloader, show image reset opacity 预加载完成后,隐藏预加载器,显示图像重置不透明度
    The function we defined for the load attribute gets called and inside the handler we reset the opacity, hide the preloader and set the src of our element in the page to the src of the Image object we created.. 我们为定义的函数load属性被调用和处理程序中,我们重新设置不透明度,隐藏预加载并设置src页面我们元件与src我们创建的图像对象的..

here is a full example: http://www.jsfiddle.net/kqC9U/ 这是一个完整的示例: http : //www.jsfiddle.net/kqC9U/

Well, lets take a quick moment and dissect the problem: 好吧,让我们花点时间分析一下问题:

If your changing the image source (which can be done almost instantly) why do you need a loader gif? 如果要更改图像源(几乎可以立即完成),为什么需要加载程序gif? In fact, displaying the loader gif takes as long as displaying the new image. 实际上,显示加载程序gif所需的时间与显示新图像所需的时间相同。 If you'd like the loader gif for dramatic appeal you can display the loader.gif, wait 5 seconds, then change to the new image. 如果您希望loader gif具有惊人的吸引力,则可以显示loader.gif,等待5秒钟,然后更改为新图像。

Changing the image can easily be accomplished by changing the src attribute on a current image. 更改图像可以轻松地通过更改当前图像上的src属性来完成。 For instance, 例如,

$.('#myCheckboxID').change(function()
{
    $.('#myPictureID').attr('src', "path/to/new/image/");
});

Let me know if you decide you need that loader gif for dramatic effect and I'll help you wire it up 让我知道是否您决定需要该gif加载器才能产生戏剧性的效果,我会帮助您进行连接

edit: Because the loading effect is desired, try something like this. 编辑:由于需要加载效果,请尝试类似的操作。 I'll pencil down some psuedocode, let me know if its not specific enough. 我将写下一些伪代码,让我知道它是否不够具体。

Change the source on your first picture
Hide the first picture for x seconds (get a feel for how long it takes to load)
Unhide a loading gif of the same size with opacity already set
hide the loading gif and unhide the first picture

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM