热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

用jqueryuifold效果替换JS动画-ReplacingJSanimatebyjqueryuifoldeffect

Imcurrentlyusingthecodebelow,whichworksgreat.HoweverImexperiencingafewissueslimtati

I'm currently using the code below, which works great. However I'm experiencing a few issues/limtations that make me think that it would be more appropriate to use jQuery UI fold effect instead of js animate function.

我目前正在使用下面的代码,效果很好。但是我遇到了一些问题/限制,这让我觉得使用jQuery UI折叠效果代替js animate函数会更合适。

As I'm not very familiar with JS could you please help me to adapt the code below to use jQuery UI fold effect rather than animate function? Many thanks

由于我对JS不是很熟悉,你可以帮我调整下面的代码来使用jQuery UI折叠效果而不是动画功能吗?非常感谢

http://jsfiddle.net/rnjea41y/

http://jsfiddle.net/rnjea41y/

JS:

JS:

$("a").click(function () {
  var page = $(this).data("page");
  if ($('div:animated').id != page) {
    var active = $(".fold.active");

    // if there is visible fold element on page (user already clicked at least once on link)
    if (active.length) {
      active.animate({
        width: "0"
      }, 200)
      .animate({
        height: "0"
      }, 200, function () {
        // this happens after above animations are complete
        $(this).removeClass("active");
      });
      // clicking for the first time
    };
    if (active.attr("id") != page) {
      $("#" + page)
      .addClass("active")
      .animate({
        height: "500px"
      }, 1000, 'linear')
      .animate({
        width: "500px"
      }, 400, 'linear')
    }
  }
});

3 个解决方案

#1


2  

Your code is pretty close to what you need. I think the main issue is your styles and understanding how they interact with the effect.

您的代码非常接近您的需求。我认为主要问题是你的风格和理解它们如何与效果相互作用。

The fold effect will show or hide elements based on their current state. You have the .fold class that starts your divs at 0x0 pixels and hidden, but what you really want is a rule that describes what your divs look like when displayed to your users.

折叠效果将根据当前状态显示或隐藏元素。你有.fold类以0x0像素开始你的div并隐藏,但你真正想要的是一个规则,描述你的div显示给用户时的样子。

Original:

原版的:

.fold {
    display: none;
    width: 0;
    height: 0;
    position: absolute;
}

Corrected:

更正:

.fold {
    width: 500px;
    height: 500px;
    position: absolute;
}

Since your .fold style is now describing the end state of your divs, it no longer has a display: none; rule. Since you want the divs to initially be hidden, you should add some Javascript to hide those initially:

由于您的.fold样式现在描述了div的结束状态,因此它不再具有display:none;规则。由于您希望最初隐藏div,因此您应该添加一些Javascript来隐藏最初的那些:

$(".fold").hide();

Now, instead of manually animating styles, you can use the fold effect:

现在,您可以使用折叠效果,而不是手动设置动画样式:

Original:

原版的:

// if there is visible fold element on page (user already clicked at least once on link)
if (active.length) {
    active.animate({
        width: "0"
    }, 200)
        .animate({
        height: "0"
    }, 200, function () {
        // this happens after above animations are complete
        $(this).removeClass("active");
    })

    // clicking for the first time
}
if (active.attr("id") != page) {
    $("#" + page)
        .addClass("active")
        .animate({
        height: "500px"
    }, 1000, 'linear')
        .animate({
        width: "500px"
    }, 400, 'linear')

}

Updated:

更新:

if (active.length) {
    active.toggle("fold", function () {
      $(this).removeClass("active");
    });
}
// clicking for the first time
if (active.attr("id") != page) {
    $("#" + page)
      .addClass("active")
      .toggle("fold");
}

Now that all of that is working, I think you'll want to play with some of the settings. The documentation for the fold effect shows that you can set the size of your element as it is folded up as well as the order of folding. To mimic the link that you posted, I would set the size to 5 since your divs have a 5px border. I would also set horizFirst to true since that is what your example shows.

现在所有这些都有效,我想你会想要玩一些设置。折叠效果的文档显示您可以设置元素折叠时的大小以及折叠顺序。为了模仿您发布的链接,我将大小设置为5,因为您的div具有5px边框。我也会将horizFirst设置为true,因为这就是您的示例所示。

I also decided to use toggleClass instead of addClass("active") and removeClass("active"). This resulted in simpler settings.

我还决定使用toggleClass而不是addClass(“active”)和removeClass(“active”)。这导致更简单的设置。

Here's the finished product:

这是成品:

$(".fold").hide();

var foldSettings = {
    easing: 'linear',
    duration: 1200,
    size: 5,
    horizFirst: true,
    complete: function () {
        $(this).toggleClass("active");
    }
}

$("a").click(function () {
    var page = $(this).data("page");
    if ($('div:animated').id != page) {
        var active = $(".fold.active");

        // if there is visible fold element on page (user already clicked at least once on link)
        if (active.length) {
            active.toggle("fold", foldSettings);
        }
        // clicking for the first time
        if (active.attr("id") != page) {
            $("#" + page).toggle("fold", foldSettings);
        }
    }
});

http://jsfiddle.net/z69zofxm/3/

http://jsfiddle.net/z69zofxm/3/

#2


2  

Edit:

编辑:

This took me enormous amount of time than expected, because of a weird CSS behaviour, so the problem was this rule which I removed:

这花费了大量的时间,因为奇怪的CSS行为,所以问题是我删除了这个规则:

.fold.active {
  display: inline-block;
}

And the solution is here : http://jsfiddle.net/3tf5ttrf/

解决方案在这里:http://jsfiddle.net/3tf5ttrf/

$("a").click(function () {
  var page = $(this).data("page");
  var selected = $("#" + page);
  var active = $('.fold.active')

  if (active.length > 0) {
      if (active.first().is(selected.first())) {
          console.log('same');
          toggleEl(selected);
      } else {
          console.log('diff');
          toggleEl(active);
          toggleEl(selected);
      }
  } else {
      console.log('zero');
      toggleEl(selected);
  }
});

function toggleEl(el) {
    el.toggleClass("active");
    el.toggle("fold");
}

#3


1  

Try this less JS more CSS

试试这个少了JS的CSS

$("li").on("click", function () {
    var dataPage = $(this).find("a").attr("data-page");
    console.log(dataPage);
    $("[id="+ dataPage + "]").addClass("active").siblings("div.fold").removeClass("active");
    $(this).addClass("active").siblings("li").removeClass("active")
});
.fold {
    width: 0px;
    height: 0px;
    overflow: hidden;
    position: absolute;
    transition: width .8s ease-in-out, height .8s .8s ease-in-out 
}
.fold.active {
    width: 500px;
    height: 500px;
    transition: height .8s ease-in-out, width .8s .8s ease-in-out
}

#fold1 {
    border: 5px solid #bada55;
    background: red
}
#fold2 {
    border: 5px solid #fed900;
    background: aqua
}
#fold3 {
    border: 5px solid #223322;
    background: green
}
#fold4 {
    border: 5px solid #55bada;
    background: purple
}
ul {
    position: absolute;
    top: 50px;
    right: 50px;
    list-style-type: none
}
li.active a{color: red}

Div menu item 1
Div menu item 2
Div menu item 3
Div menu item 4


推荐阅读
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 本文介绍了如何使用jQuery和AJAX来实现动态更新两个div的方法。通过调用PHP文件并返回JSON字符串,可以将不同的文本分别插入到两个div中,从而实现页面的动态更新。 ... [详细]
  • Android实战——jsoup实现网络爬虫,糗事百科项目的起步
    本文介绍了Android实战中使用jsoup实现网络爬虫的方法,以糗事百科项目为例。对于初学者来说,数据源的缺乏是做项目的最大烦恼之一。本文讲述了如何使用网络爬虫获取数据,并以糗事百科作为练手项目。同时,提到了使用jsoup需要结合前端基础知识,以及如果学过JS的话可以更轻松地使用该框架。 ... [详细]
  • 单页面应用 VS 多页面应用的区别和适用场景
    本文主要介绍了单页面应用(SPA)和多页面应用(MPA)的区别和适用场景。单页面应用只有一个主页面,所有内容都包含在主页面中,页面切换快但需要做相关的调优;多页面应用有多个独立的页面,每个页面都要加载相关资源,页面切换慢但适用于对SEO要求较高的应用。文章还提到了两者在资源加载、过渡动画、路由模式和数据传递方面的差异。 ... [详细]
  • 本文介绍了Java后台Jsonp处理方法及其应用场景。首先解释了Jsonp是一个非官方的协议,它允许在服务器端通过Script tags返回至客户端,并通过javascript callback的形式实现跨域访问。然后介绍了JSON系统开发方法,它是一种面向数据结构的分析和设计方法,以活动为中心,将一连串的活动顺序组合成一个完整的工作进程。接着给出了一个客户端示例代码,使用了jQuery的ajax方法请求一个Jsonp数据。 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • MySQL中的MVVC多版本并发控制机制的应用及实现
    本文介绍了MySQL中MVCC的应用及实现机制。MVCC是一种提高并发性能的技术,通过对事务内读取的内存进行处理,避免写操作堵塞读操作的并发问题。与其他数据库系统的MVCC实现机制不尽相同,MySQL的MVCC是在undolog中实现的。通过undolog可以找回数据的历史版本,提供给用户读取或在回滚时覆盖数据页上的数据。MySQL的大多数事务型存储引擎都实现了MVCC,但各自的实现机制有所不同。 ... [详细]
  • 本文介绍了在满足特定条件时如何在输入字段中使用默认值的方法和相应的代码。当输入字段填充100或更多的金额时,使用50作为默认值;当输入字段填充有-20或更多(负数)时,使用-10作为默认值。文章还提供了相关的JavaScript和Jquery代码,用于动态地根据条件使用默认值。 ... [详细]
  • jQuery实现简单的动画效果及用法详解
    本文详细介绍了使用jQuery实现简单动画效果的方法,包括显示/隐藏、向上收缩/向下展开、淡入/淡出、自定义动画等。同时提供了具体的用法示例,并解释了参数的含义和使用技巧。通过本文的学习,读者可以掌握如何使用jQuery实现各种动画效果,为网页增添生动和互动性。 ... [详细]
  • jQuery如何判断一个元素是否被点击?
    本文介绍了使用jQuery判断一个元素是否被点击的方法,并通过示例进行了具体说明。注意要指定父级,否则会执行多次。 ... [详细]
author-avatar
地之南_816
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有