平滑这个 jQuery 切换动画吗?

发布于 2024-12-29 05:33:29 字数 1265 浏览 0 评论 0原文

我的 jQuery 函数生成的动画不稳定,我一直在寻找不同的 SO 解决方案,例如添加 jquery.easing,但没有运气。问题是每个 div 中的 iframe 吗?

关于如何平滑动画有什么想法吗?我的基本切换功能是最好的方法吗?

JSFiddle: http://jsfiddle.net/gwLcD/8/

<强>基本标记如下,并在页面上重复多次(每个“videotoggle”div之间有文本块):

 <div class="videotoggle">

    <p><h2 class="entry-title">View a few minutes of the (title) video </h2></p>

    <div class="videoblock">

    <iframe width="560" height="315" src="http://www.youtube.com/embed/????????"
    frameborder="0" allowfullscreen></iframe>

    </div></div>

功能:

$(document).ready(function(){
$(".videoblock").hide();  //closes all divs on first page load
$(".entry-title").click(function() {
    $this = $(this);  //this next code only allows one open div at a time
    $content = $this.closest('.videotoggle').find(".videoblock");
    if (!$this.is('.active-title')) {
        $('.active-title').removeClass('active-title');
        $this.addClass('active-title');
        $(".videoblock:visible").slideToggle(400);  //slide toggle
        $content.slideToggle(400);
    }
});
});

The animation produced by my jQuery function is shaky, and I've been looking through different SO solutions, such as adding jquery.easing, but no luck. Is the problem the iframes in each div?

Any ideas on how to smooth out the animation? Is my basic toggle function the best way?

JSFiddle: http://jsfiddle.net/gwLcD/8/

The basic markup is below, and is repeated numerous times on the page (with blocks of text in between each "videotoggle" div):

 <div class="videotoggle">

    <p><h2 class="entry-title">View a few minutes of the (title) video </h2></p>

    <div class="videoblock">

    <iframe width="560" height="315" src="http://www.youtube.com/embed/????????"
    frameborder="0" allowfullscreen></iframe>

    </div></div>

And the function:

$(document).ready(function(){
$(".videoblock").hide();  //closes all divs on first page load
$(".entry-title").click(function() {
    $this = $(this);  //this next code only allows one open div at a time
    $content = $this.closest('.videotoggle').find(".videoblock");
    if (!$this.is('.active-title')) {
        $('.active-title').removeClass('active-title');
        $this.addClass('active-title');
        $(".videoblock:visible").slideToggle(400);  //slide toggle
        $content.slideToggle(400);
    }
});
});

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

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

发布评论

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

评论(5

哽咽笑 2025-01-05 05:33:29

安德鲁的解决方案是正确的,但我仍然会像这样放置CSS(如果javascript关闭):
.videoblock{宽度:560px;高度:315 像素;溢出:隐藏}

并添加同步动画:

$('.videoblock').css('height','0');
$(".entry-title").click(function() {
    $this = $(this);
    $content = $this.closest('.videotoggle').find(".videoblock");
    if (!$this.is('.active-title')) {

        $('.active-title').removeClass('active-title');
        $this.addClass('active-title');
        $(".videoblock:visible").animate({height: "0"}, { duration: 400, queue: false });
        $content.animate({height: "315"}, { duration: 400, queue: false });
    }
});

这是最终的链接:http://jsfiddle.net/gwLcD/21/< /a>

Andrew's solution is correct, but I would still put the css like this (if javascript is off):
.videoblock{width:560px; height: 315px; overflow:hidden}

and add the simultaneous animation:

$('.videoblock').css('height','0');
$(".entry-title").click(function() {
    $this = $(this);
    $content = $this.closest('.videotoggle').find(".videoblock");
    if (!$this.is('.active-title')) {

        $('.active-title').removeClass('active-title');
        $this.addClass('active-title');
        $(".videoblock:visible").animate({height: "0"}, { duration: 400, queue: false });
        $content.animate({height: "315"}, { duration: 400, queue: false });
    }
});

Here is the link to the final: http://jsfiddle.net/gwLcD/21/

薄暮涼年 2025-01-05 05:33:29

看看这个 - http://jsfiddle.net/vbXVR/

它使用这个jquery

$(document).ready(function(){
    $(".entry-title").click(function() {
        $(".videoblock").animate({height: "315"}, 1500);
    });
});

check this out - http://jsfiddle.net/vbXVR/.

it uses this jquery

$(document).ready(function(){
    $(".entry-title").click(function() {
        $(".videoblock").animate({height: "315"}, 1500);
    });
});
尤怨 2025-01-05 05:33:29

我们这样看吧!

我不确定您将在此处的一页上加载多少个 iframe,但有一件事似乎非常确定:如果您有太多,您将有足够的 iframe 和足够的 YouTube 视频加载。

这意味着不必要的负载。用户可能不会单击所有这些链接。用户可能不会观看所有这些视频。

因此:

  1. 存在不必要的资源膨胀,以及不必要的用户带宽消耗。

  2. 而且,这是不可扩展的。考虑一下,您在一个页面上需要 50 个这样的链接。好的。只取 10 个。即使这样也相当多了!

我正在为此制作一个jsfiddle。完成后将其发布在这里!

Let's look at it this way!

I am not sure how many of these iframes you're going to load up on one page here, but one thing seems to be very certain; if you have one too many, you'll have enough of iframes with enough of youtube videos loading up.

Which means, unnecessary loads. The user may not click all of those links. The user may not watch all of those videos.

So:

  1. There's an unnecessary bloat up of the resources, and an unnecessary consumption of the user's bandwidth.

  2. And, moreover this is not scalable. Consider, you need 50 of those links on a page. OK. Take just 10. Even that is quite a lot!

I'm working up a jsfiddle for this one. Will post it here, when done!

疾风者 2025-01-05 05:33:29

您主要使用哪种浏览器?如果它是任何 webkit 浏览器或 FireFox,那么您可以利用带有 jquery 后备的硬件加速 CSS3 转换。

http://msdn.microsoft.com/en-us/scriptjunkie/hh304380

我不认为 jQuery 缓动当前使用 CSS3 转换作为第一个选项,但如果我错了,请纠正我。

看看:
http://css3.bradshawenterprises.com/all/

根本不需要太多努力使用 CSS3 来破解一些东西。

What browser are you primarily gunning for? If it's any of the webkit browsers or FireFox then you could take advantage of the hardware accelerated CSS3 transitions with a jquery fallback.

http://msdn.microsoft.com/en-us/scriptjunkie/hh304380

I don't think jQuery easing currently uses CSS3 transitions as a first option but correct me if I am wrong.

Take a look at:
http://css3.bradshawenterprises.com/all/

It wouldn't be too much effort at all to hack something up using CSS3.

风铃鹿 2025-01-05 05:33:29

您不想直接使用手风琴插件的具体原因是什么? jQuery UI 库应该很好地处理这个问题。

另外,如果不能按预期工作,你可以尝试 css3 动画吗?您可以在这里获取 CSS3 动画的要点: http://titansturf.in/ 2012/01/12/using-css3-transitions/

您必须创建两个类,其中一个带有 div-hide,它具有height: 0 和带有 div-show 的一个,其中设置了所需的 height 。每当您想要切换时,只需使用 jQuery 更改类即可。

IMO,如果您的目标受众使用现代浏览器,那么使用 CSS3 将是一个不错的选择。如果没有,您可以使用 Modernizr 根据所使用的浏览器类型来更改工作方式。

Any specific reason you do not want to use an accordion plugin directly? The jQuery UI library should take care of this pretty nicely.

Also, in case that does not work as expected, can you try css3 animations? You can get a gist of CSS3 animations here: http://titansturf.in/2012/01/12/using-css3-transitions/

You will have to create two classes, one with div-hide, which has height: 0 and one with div-show which has the required height set. Whenever you want to toggle, just change the class using jQuery.

IMO, using CSS3 would be a good options incase your target audience uses modern browsers. If not, you can use Modernizr to change the way things work according to the kind of browser being used.

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