【发布时间】:2017-03-19 16:52:44
【问题描述】:
我正在使用 JavaScript 来切换通知,如下所示。
如何在display: block 和display: none; 之间添加过渡
我不想添加像 jQuery 这样的外部库,因为我只会单独使用 toggle 效果。
var btn = document.querySelector('button');
btn.addEventListener('click', function(){
var hint = document.getElementById('hint');
if(hint.style.display == 'none'){
hint.style.display = 'block';
}
else{
hint.style.display = 'none';
}
});
div#hint{
background: gold;
color: orangered;
padding: .5em;
font-weight: bold;
}
<div id='hint'>
<p>This is some hint on how to be safe in this community </p>
<p>This is another hint on how to be safe in this community </p>
</div>
<button> show hint </button>
我知道我可以使用 jQuery 来实现这一点,如下所示。
$(document).ready(function(){
$('button').click(function(){
$('#hint').toggle('slow');
});
});
div#hint{
background: gold;
color: orangered;
padding: .5em;
font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='hint'>
<p>This is some hint on how to be safe in this community </p>
<p>This is another hint on how to be safe in this community </p>
</div>
<button> show hint </button>
在#hint 像上面的jQuery 示例中那样切换时,我可以让按钮逐渐上下移动吗?我不希望按钮从一个位置跳转到另一个位置。
【问题讨论】:
标签: javascript transition display