老师帮我解答下吧 为什么实现不了淡入淡出效果?
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="./css/base.css"> <link rel="stylesheet" href="./font/iconfont.css"> <title>dropDown</title> <style> .dropdown { position: relative; /* margin: 150px; */ } .dropdown-toggle { position: relative; z-index: 2; } /* 下拉箭头 */ .dropdown-arrow { display: inline-block; /* text-align: center; */ vertical-align: text-top; } .dropdown-layer { display: none; overflow: hidden; position: absolute; z-index: 1; } .dropdown-left { left: 0; right: auto; } .dropdown-right { right: 0; left: auto; } /* menu dropdown */ .menu .dropdown-toggle { display: block; height: 100%; padding: 0 16px 0 12px; border-left: 1px solid #f3f5f7; border-right: 1px solid #f3f5f7; } .menu .dropdown-arrow { margin-left: 5px; } .menu .dropdown-layer { /* top: 44px; */ top: 100%; border: 1px solid #cdd0d4; background-color: #fff; } .menu-item { display: block; height: 30px; padding: 0 12px; line-height: 30px; white-space: nowrap; color: #4d555d; } .menu-item:hover { background-color: #f3f5f7; } /* css hover 实现显示隐藏 */ .menu-active .dropdown-toggle { background-color: #fff; border-color: #cdd0d4; } /* 下拉箭头效果 */ [class *= "-active"] .dropdown-arrow { -o-transform: rotate(180deg); -ms-transform: rotate(180deg); -moz-transform: rotate(180deg); -webkit-transform: rotate(180deg); transform: rotate(180deg); } .transition { -o-transition: all 0.5s; -ms-transition: all 0.5s; -moz-transition: all 0.5s; -webkit-transition: all 0.5s; transition: all 0.3s; } /* .menu-active .dropdown-layer { display: block; } */ /* 显示隐藏效果 */ .fadeOut { visibility: hidden !important; opacity: 0 !important; } .slideUpDownCollapse { height: 0 !important; padding-top: 0 !important; padding-bottom: 0 !important; } .slideLeftRightCollapse { width: 0 !important; padding-left: 0 !important; padding-right: 0 !important; } </style> </head> <body> <div class="menu dropdown fl" data-active="menu"> <a href="" class="dropdown-toggle">我的慕淘<i class="iconfont dropdown-arrow transition"></i></a> <ul class="dropdown-layer dropdown-left"> <li><a href="###" class="menu-item">已买到宝贝</a></li> <li><a href="###" class="menu-item">我的足迹</a></li> </ul> </div> <script src="./js/jquery.js"></script> <script src="./js/transition.js"></script> <script src="./js/showHide.js"></script> <script src="./js/dropDown.js"></script> <script> $('.dropdown').dropdown({ css3: true, js: true, animation: 'slideUpDown' }); </script> </body> </html>
JS showHide
(function ($) { 'use strict' var transition = window.mt.transition; function init($elem, hiddenCallback) { if ($elem.is(':hidden')) { $elem.data('status', 'hidden'); if (typeof hiddenCallback === 'function') hiddenCallback(); } else { $elem.data('status', 'shown'); } } // 提取 show hide 公共部分 function show($elem, callback) { if ($elem.data('status') === 'show') return; if ($elem.data('status') === 'shown') return; $elem.data('status', 'show').trigger('show'); callback(); } function hide($elem, callback) { if ($elem.data('status') === 'hide') return; if ($elem.data('status') === 'hidden') return; $elem.data('status', 'hide').trigger('hide'); callback(); } var silent = { init: init, show: function ($elem) { show($elem, function () { $elem.show(); $elem.data('status', 'shown').trigger('shown'); }); }, hide: function ($elem) { hide($elem, function () { $elem.hide(); $elem.data('status', 'hidden').trigger('hidden') }); } }; var css3 = { fade: { init: function ($elem) { css3._init($elem, 'fadeOut'); }, show: function ($elem) { css3._show($elem, 'fadeOut') }, hide: function ($elem) { css3._hide($elem, 'fadeOut') } }, slideUpDown: { init: function ($elem) { $elem.height($elem.height()); css3._init($elem, 'slideUpDownCollapse'); }, show: function ($elem) { css3._show($elem, 'slideUpDownCollapse') }, hide: function ($elem) { css3._hide($elem, 'slideUpDownCollapse') } }, slideLeftRightCollapse: { init: function ($elem) { $elem.width($elem.width()); css3._init($elem, 'slideLeftRightCollapse'); }, show: function ($elem) { css3._show($elem, 'slideLeftRightCollapse') }, hide: function ($elem) { css3._hide($elem, 'slideLeftRightCollapse') } }, fadeslideUpDown: { init: function ($elem) { $elem.height($elem.height()); css3._init($elem, 'fadeOut slideUpDownCollapse'); }, show: function ($elem) { css3._show($elem, 'fadeOut slideUpDownCollapse') }, hide: function ($elem) { css3._hide($elem, 'fadeOut slideUpDownCollapse') } }, fadeslideLeftRight: { init: function ($elem) { $elem.width($elem.width()); css3._init($elem, 'fadeOut slideLeftRightCollapse'); }, show: function ($elem) { css3._show($elem, 'fadeOut slideLeftRightCollapse') }, hide: function ($elem) { css3._hide($elem, 'fadeOut slideLeftRightCollapse') } } }; css3._init = function ($elem, className) { $elem.addClass('transiton'); init($elem, function () { $elem.addClass(className); }) }; css3._show = function ($elem, className) { show($elem, function () { $elem.off(transition.end).one(transition.end, function () { $elem.data('status', 'shown').trigger('shown'); }); $elem.show(); setTimeout(function () { $elem.removeClass(className); }, 20) }); }; css3._hide = function ($elem, className) { hide($elem, function () { $elem.off(transition.end).one(transition.end, function () { $elem.hide(); $elem.data('status', 'hidden').trigger('hidden'); }) $elem.addClass(className); }); }; var js = { fade: { init: function ($elem) { js._init($elem); }, show: function ($elem) { js._show($elem, 'fadeIn'); }, hide: function ($elem) { js._hide($elem, 'fadeOut') } }, slideUpDown: { init: function ($elem) { js._init($elem); }, show: function ($elem) { js._show($elem, 'slideDown'); }, hide: function ($elem) { js._hide($elem, 'slideUp') } }, // 因为jQuery没有封装slideLeftRight 需要自己写的JS动画 slideLeftRight: { init: function ($elem) { js._customInit($elem, { 'width': 0, 'padding-left': 0, 'padding-right': 0, }); }, show: function ($elem) { js._customShow($elem); }, hide: function ($elem) { js._customHide($elem, { 'width': 0, 'padding-left': 0, 'padding-right': 0, }); } }, fadeslideUpDown: { init: function ($elem) { js._customInit($elem, { 'opacity': 0, 'height': 0, 'padding-top': 0, 'padding-bottom': 0, }); }, show: function ($elem) { js._customShow($elem); }, hide: function ($elem) { js._customHide($elem, { 'opacity': 0, 'height': 0, 'padding-top': 0, 'padding-bottom': 0, }); } }, fadeslideLeftRight: { init: function ($elem) { js._customInit($elem, { 'opacity': 0, 'width': 0, 'padding-left': 0, 'padding-right': 0, }); }, show: function ($elem) { js._customShow($elem); }, hide: function ($elem) { js._customHide($elem, { 'opacity': 0, 'width': 0, 'padding-left': 0, 'padding-right': 0, }); } } }; js._init = function ($elem, hiddenCallback) { $elem.removeClass('transition'); init($elem, hiddenCallback); }; js._customInit = function ($elem, options) { var styles = {}; for (var p in options) { styles[p] = $elem.css(p); } $elem.data('styles', styles) js._init($elem, function () { $elem.css(options); }) }; js._show = function ($elem, mode) { show($elem, function () { $elem.stop()[mode](function () { $elem.data('status', 'shown').trigger('shown'); }); }); }; js._customShow = function ($elem) { show($elem, function () { $elem.show(); $elem.stop().animate($elem.data('styles'), function () { $elem.data('status', 'shown').trigger('shown'); }); }) } js._hide = function ($elem, mode) { hide($elem, function () { $elem.stop()[mode](function () { $elem.data('status', 'hidden').trigger('hidden'); }); }); }; js._customHide = function ($elem, options) { hide($elem, function () { $elem.stop().animate(options, function () { $elem.hide(); $elem.data('status', 'hidden').trigger('hidden'); }); }) } var defaults = { css3: true, js: true, animation: 'fade' }; function showHide($elem, options) { var mode = null; if (options.css3 && transition.isSupport) { // css3 transition mode = css3[options.animation] || css3[defaults.animation]; } else if (options.js) { // js animation mode = js[options.animation] || js[defaults.animation]; } else { // no animation mode = silent; } mode.init($elem); return { show: $.proxy(mode.show, this, $elem), hide: $.proxy(mode.hide, this, $elem) } }; $.fn.extend({ showHide: function (option) { return this.each(function () { var $this = $(this), options = $.extend({}, defaults, typeof option === 'object' && option), mode = $this.data('showHide'); if (!mode) { $this.data('showHide', mode = showHide($this, options)); } if (typeof mode[option] === 'function') { mode[option](); } }); } }) // window.mt = window.mt || {}; // window.mt.showHide = showHide; })(jQuery);
JS dropDown
(function ($) { 'use strict'; function dropdown(elem, options) { var $elem = $(elem), $layer = $elem.find('.dropdown-layer'), activeClass = $elem.data('active') + '-active'; $layer.showHide(options); $elem.hover(function () { $elem.addClass(activeClass); $layer.showHide('show'); }, function () { $elem.removeClass(activeClass); $layer.showHide('hide'); }); }; var defaults = { css3: true, js: true, animation: 'slideUpDown' }; $.fn.extend({ dropdown: function (option) { return this.each(function () { var options = $.extend({}, defaults, option); dropdown(this,options); }); } }); })(jQuery);
28
收起
正在回答
6回答
同学你好,问题解答如下:
1、移出效果有问题,那么就去相对应的隐藏中找,也就是这里
移入移出的时候只输出了111和11:
说明绑定的transition.end事件没有进去,那么就需要考虑transition.js文件中问题了,这样按照代码执行顺序,逻辑顺序一点点的去找就好了。
2、fade效果中其实也没有执行transition.end事件,和slideUpDown动画执行过程是一样的,只不过fade效果中有fadeOut类,设置了看不见和透明度为0:
这样达到隐藏的效果。
如果同学还有其它问题,建议在问答区新建问答提问,当前问题盖楼较多,不利于同学后期查找复习。如果我的回答帮助到了你,欢迎采纳,祝学习愉快~
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星