CSS3 右侧悬浮 DIV 面板是一种常见的网页设计元素,我们可以通过 CSS3 的 transform属性和 transition属性实现这一效果。首先,我们需要一个 DIV 容器,并设置它的样式为...
CSS3 右侧悬浮 DIV 面板是一种常见的网页设计元素,我们可以通过 CSS3 的 transform属性和 transition属性实现这一效果。
首先,我们需要一个 DIV 容器,并设置它的样式为:
.container {
position: fixed;
right: 0;
top: 50%;
transform: translateY(-50%);
transition: all .3s ease-out;
} 这里我们将容器定位到页面的右侧,然后使用 transform属性将容器向上偏移了一半的高度,在垂直方向上居中显示。transition属性则用于定义容器的过渡效果。
下一步,我们需要设置容器的默认显示状态和交互效果。具体代码如下:
.container {
display: none;
width: 200px;
height: 300px;
background-color: #fff;
border: 1px solid #ccc;
}
.show {
display: block;
transform: translateX(0);
}
.container:hover {
box-shadow: 2px 2px 5px #ccc;
} 这里我们定义了容器的基本样式,包括宽高、背景色和边框等,然后将默认的显示状态设置为 none。接着,我们为容器添加了一个 show类,当用户进行一定的交互行为时,通过修改容器的类名实现容器的显示。
为了增加用户体验,我们还为容器添加了鼠标悬停时的阴影效果。
最后,我们需要为触发容器显示的元素添加交互行为,具体代码如下:
.button {
position: fixed;
right: 20px;
bottom: 20px;
width: 50px;
height: 50px;
background-color: #111;
border-radius: 50%;
cursor: pointer;
transition: all .3s ease-out;
}
.button:hover {
background-color: #222;
transform: scale(1.1);
}
.button:active + .container {
transform: translateX(200px);
} 这里我们使用了一个圆形按钮作为触发按钮,并设置它的样式、位置和鼠标指针样式。同样地,我们也为按钮添加了交互效果,当用户将鼠标悬浮在按钮上时,按钮会放大并改变背景颜色。
最后,我们为按钮添加了点击事件,并使用相对定位和 transform属性,让容器从右边滑动到左边显示出来。