为什么我绑定了transition只是没有动画了但还是可以实现显示和隐藏?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>显示隐藏模块</title>
<link rel="stylesheet" href="../css/base.css" />
<style>
body {
width: 400px;
margin: 0 auto;
}
.btn {
width: 50%;
height: 30px;
}
#box {
/*display: none;*/
/*position: absolute;*/
/* visibility: hidden;
opacity: 0;*/
width: 100%;
/*height: 200px;
padding:100px 0;*/
background-color: red;
overflow: hidden;
}
.transition {
-o-transition: all 0.5s;
-ms-transition: all 0.5s;
-moz-transition: all 0.5s;
-webkit-transition: all 0.5s;
transition: all 0.5s;
}
.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>
<button id="btn-show" class="btn">显示</button><button id="btn-hide" class="btn">隐藏</button>
<div id="box" class="transition">
<p>12</p>
<p>12</p>
<p>12</p>
<p>12</p>
<p>12</p>
</div>
<script src="../js/jquery.js"></script>
<script src="../js/transition.js"></script>
<script src="../js/showHide.js"></script>
<script>
// console.log(window.mt.transition.end);
// console.log(window.mt.transition.isSupport);
var $box = $('#box');
js.fade.init($box);
$('#btn-show').on('click', function () {
js.fade.show($box);
});
$('#btn-hide').on('click', function () {
js.fade.hide($box);
});
$box.on('show shown hide hidden', function (e) {
console.log(e.type);
});
</script>
</body>
</html>
(function () {
var transitionEndEventName = {
transition: 'transitionend',
MozTransition: 'transitionend',
WebkitTransition: 'webkitTransitionEnd',
OTransition: 'oTransitionEnd otransitionend',
};
var transitionEnd = '',
isSupport = false;
for (var name in transitionEndEventName) {
if (document.body.style[name]!==undefined) {
transitionEnd = transitionEndEventName[name];
isSupport = true;
break;
}
}
window.mt = window.mt || {};
window.mt.transition = {
end: transitionEnd,
isSupport: isSupport
};
})();
var transition = window.mt.transition;
function init($elem, hiddenCallback) {
if ($elem.is(':hidden')) { //hidden
$elem.data('status', 'hidden');
if (typeof hiddenCallback === 'function') hiddenCallback();
} else { //shown
$elem.data('status', 'shown');
}
}
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');
}
},
slideLeftRight: {
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('transition');
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) {
init($elem);
},
show: function ($elem) {
show($elem, function () {
$elem.stop().fadeIn(function () {
$elem.data('status', 'shown').trigger('shown');
});
});
},
hide: function ($elem) {
hide($elem, function () {
$elem.stop().fadeOut(function () {
$elem.data('status', 'hidden').trigger('hidden');
});
});
}
},
slideUpDown: {
show: function () {
},
hide: function () {
}
},
slideLeftRight: {
show: function () {
},
hide: function () {
}
},
fadeSlideUpDown: {
show: function () {
},
hide: function () {
}
},
fadeSlideLeftRight: {
show: function () {
},
hide: function () {
}
}
};
正在回答
同学你好,代码和视频中设置的一样,没有问题。课程中老师进行的显示和隐藏快速点击切换,所以会出现效果上的问题,无法根据点击按钮进行正常显示与隐藏,但是也不是不能实现显示与隐藏的,可以再看一下老师的操作哦。
祝学习愉快!
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星