.animate() 和缩放函数

发布于 2024-12-20 12:51:49 字数 1545 浏览 3 评论 0原文

我需要创建一个取决于多个因素的画廊,并执行一系列缩放以适合的图像。无论如何,我需要当您单击“触发”按钮时,主容器几乎占据视口的整个可见区域,并且它包含相当比例的图像。

我按照这个参考

我不能做的行为是放大图像。当您单击其中之一时,拇指会消失并且主图像会缩放。我的代码如下:

我用来放大图像的函数是基于我读到的有关 jQuery .animate() 方法的一篇小文章。

function resize(){ 
gallery = $('#items'); 
galleryH = gallery.height(); 

$('#items img').each(function() { 
    var height = $(this).height(); 
    $(this).stop(true).animate({'height':galleryH},1000); 
    }); 
} 
resize();

根据这种仅对高度进行“动画”处理的方法,宽度会自动保持,从而保持比例。这似乎工作正常。正如您在上面看到的,它是根据容器的高度进行缩放的。

嗯,这在页面加载和缩放窗口大小时效果很好。然后我需要的是,当您单击容器或其他按钮/触发器填充窗口时,类似于所附示例中发生的情况,并且图像根据此大小进行缩放。正如示例中所示。为此,请使用以下代码:

function changeView(){ 

    if(gallery.hasClass("largeExp")){ 
        gallery.removeClass("largeExp"); 
            gallery.stop(true).animate({'height':'200px'},500); 
            console.log("Normal Mode"); 
        } 
        else{ 
            gallery.addClass("largeExp"); 
            gallery.stop(true).animate({'height':'650px'},500); 
            console.log("Expand Mode"); 
        } 

        console.log("Now would launch resize()"); 
        resize(); 
} 

$('#items').click(function() { 
    changeView(); 
});

好吧,会发生的情况是图库(容器)实际上对此类措施进行了“动画”处理,但其中不包含任何图像。 resize() 方法仅从第二次单击开始运行。在第一次单击中,效果很好,并且可以根据需要进行缩放,在第二次单击中,图像会缩放到大尺寸,而图库(容器)会移动到小或正常模式。从这里开始,容器和图像以相反的方式缩放。当容器处于正常模式时,图像处于展开状态,等等。工作时画廊(容器)和图像永远不适合。我需要当容器扩展时图像也扩展。

我在 JS 方面没有真正的经验,我确信我做错了什么。所以我希望你能帮助我,我为此浪费了很多时间!

预先非常感谢您!

I need to create a gallery that depends on several factors and performs a series of images scaled to fit. Anyway, I need that when you click on a "trigger" button main container occupies almost the entire visible area of the viewport and the images it contains respectable scale if ratio.

I am following this reference

The behavior that I can not do is to enlarge the image. When you click on one of them the thumbs disappear and the main image is scaled. This my code is as follows:

The function I use to enlarge the images I do based on a small article I read about the jQuery .animate() method.

function resize(){ 
gallery = $('#items'); 
galleryH = gallery.height(); 

$('#items img').each(function() { 
    var height = $(this).height(); 
    $(this).stop(true).animate({'height':galleryH},1000); 
    }); 
} 
resize();

According to this method "animating" only the height, the width is automatically thereby maintaining the ratio. This seems to work properly. As you see above is scaled according to the height of its container.

Well, this works well when the page loads and when scaling the size of the window. Then I need is that when you click on the container or other button/firer fills the window, similar to what happens in the example attached, and images are scaled according to this size. As in the example. To do this use the following code:

function changeView(){ 

    if(gallery.hasClass("largeExp")){ 
        gallery.removeClass("largeExp"); 
            gallery.stop(true).animate({'height':'200px'},500); 
            console.log("Normal Mode"); 
        } 
        else{ 
            gallery.addClass("largeExp"); 
            gallery.stop(true).animate({'height':'650px'},500); 
            console.log("Expand Mode"); 
        } 

        console.log("Now would launch resize()"); 
        resize(); 
} 

$('#items').click(function() { 
    changeView(); 
});

Well, what happens is that the gallery (the container) actually "animate" to such measures but no images contained within. The resize() method, only begins to run from the second click. In the first click is great and scales as I need, in the second click images are scaled to the large size while the gallery (the container) moves to small or normal mode. From here the container and the images scales in a inverse way. When the container is in normal mode the images are in expanded and so. Working so the gallery (container) and images never fit. I need that when container is to expanded the images go to expanded too.

I have not really experience in JS and I'm sure I'm doing something wrong. So I hope you can help me, I've lost a lot of hours with this!

Thank you very much in advance!

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

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

发布评论

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

评论(1

只有影子陪我不离不弃 2024-12-27 12:51:49

我现在在 iPad 上,所以我无法真正复制代码,但我现在只能说这似乎是一个时间问题。发生的情况是,您单击缩放图库的按钮,实际上会在启动缩放图库的动画的同时调用调整大小函数,该动画需要 500 毫秒。您需要在调整大小函数上设置大于 500 毫秒的时间,或者在 animate 方法内调用该函数。基本上,您必须在图库重新缩放完成后调用调整大小函数。

setTimeout(function() { resize(); }, 510);

I am on my iPad right now so I can't really replicate the code but all I can say for now is that it seems like a timing issue. What happens is you click the button to scale the gallery, the resize function is called practically at the same time the animation to scale the gallery which takes 500 ms is launched. You need to either set a time out of greater that 500 ms on the resize function or call the function inside of the animate method. Basically you have to call the resize function after the gallery rescaling is complete.

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