我正在使用 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 示例一样吗?我不希望按钮从一个位置跳到另一个位置。