我试图在单击按钮时显示带有文字和图像的条(好像是通知),该条会一直增长直到达到最大大小并显示内容为止,但是当动画发生时正在重新定位,我希望它停在原位,只有条形物才能移动。
<!DOCTYPE html>
<html>
<head>
<style>
.notification {
width: 0px;
height: 100px;
background: whitesmoke;
display: flex;
overflow: hidden;
-webkit-transition: width 2s;
transition: width 2s;
}
.show {
width: 450px;
}
.avatar img {
width: 70px;
height: 70px;
margin: 20px;
}
.text {
margin: 5px 20px;
}
</style>
</head>
<body>
<div class="notification">
<div class="avatar">
<img src="https://cdn.icon-icons.com/icons2/1147/PNG/512/1486486303-alert-bell-notification-education-christmas-bell-church-bell-ring_81235.png">
</div>
<div class="text">
<p><strong>Call Notification</strong></p>
<p>Receive a notification by a determinate person - <strong>PERSON NAME</strong></p>
</div>
</div>
<br><br>
<button>Notify</button>
<script type="text/javascript">
let notification = document.querySelector('.notification');
document.querySelector('button').addEventListener('click', function(){
notification.classList.add('show');
});
</script>
</body>
</html>
我尝试添加空白:nowrap属性。
.text {
white-space: nowrap;
margin: 5px 20px;
}
它甚至可以按我想要的方式工作,但是如果文本太大(大于栏的宽度),则会被overflow:hidden属性隐藏。
<!DOCTYPE html>
<html>
<head>
<style>
.notification {
width: 0px;
height: 100px;
background: whitesmoke;
display: flex;
overflow: hidden;
-webkit-transition: width 2s;
transition: width 2s;
}
.show {
width: 450px;
}
.avatar img {
width: 70px;
height: 70px;
margin: 20px;
}
.text {
white-space: nowrap;
margin: 5px 20px;
}
</style>
</head>
<body>
<div class="notification">
<div class="avatar">
<img src="https://cdn.icon-icons.com/icons2/1147/PNG/512/1486486303-alert-bell-notification-education-christmas-bell-church-bell-ring_81235.png">
</div>
<div class="text">
<p><strong>Call Notification</strong></p>
<p>Receive a notification by a determinate person - <strong>PERSON NAME</strong></p>
</div>
</div>
<br><br>
<button>Notify</button>
<script type="text/javascript">
let notification = document.querySelector('.notification');
document.querySelector('button').addEventListener('click', function(){
notification.classList.add('show');
});
</script>
</body>
</html>
有人知道如何帮助我解决这个问题吗?
答案 0 :(得分:1)
您可以将文本设置为固定宽度并禁用其收缩效果,以保持该固定宽度,从而使文本不会移动,只需显示即可:
ind
let notification = document.querySelector('.notification');
document.querySelector('button').addEventListener('click', function() {
notification.classList.add('show');
});
.notification {
width: 0px;
height: 100px;
background: whitesmoke;
display: flex;
overflow: hidden;
transition: width 2s;
}
.show {
width: 450px;
}
.avatar img {
width: 70px;
height: 70px;
margin: 20px;
}
.text {
margin: 5px 20px;
width:calc(450px - 70px - 2*20px); /*total width - image width - margin of image*/
flex-shrink:0; /* avoid the width of the element to be reduced by the shrink effect*/
}