本篇分别用jQuery和原生两种方式实现悬浮窗功能和样式的切换,兼容IE
完成样式示例




使用技术栈:jquery、javaScript、HTML/CSS
一、基础版面
悬浮窗可以有很多样式,但基础结构可以大致分为触发按钮、遮罩层、内容三部分。触发按钮通常设置在屏幕边缘,我这里举例一个右下的位置展示;
<div class="Suspended_window_box">
<img src="../image/map_image/icon.png" alt="" />
<p>联系我们</p>
</div>
<style>
/* 悬浮窗触发按钮样式 */
.Suspended_window_box{
width: 150px;
height: 70px;
border-radius: 50px 0 0 50px;
overflow: hidden;
background-color: #ebebeb;
position: fixed;
bottom: 100px;
right: -70px;
transition: .3s all; /* 动画运行时间 */
z-index: 198;
}
/* 悬浮窗按钮图片 */
.Suspended_window_box img{
width: auto;
height: 80%;
margin-top: 7px;
margin-left: 7px;
transition: .3s all;
float: left;
}
/* 悬浮窗按钮文字 */
.Suspended_window_box p{
opacity: 0;
line-height: 70px;
float: right;
font-size: 20px;
font-weight: bold;
transition: .3s all;
}
/* 悬浮窗按钮触发 */
.Suspended_window_box:hover{
cursor: pointer;
right: -0px;
background: #ccc;
}
.Suspended_window_box:hover p{
opacity: 1;
}
/* 悬浮窗按钮点击后类名 */
.toclick{
opacity: 0;
}
</style>
这样,一个在右下角的圆角长条按钮就完成了,接下来是悬浮窗的遮罩层部分,这里应该不用我过多赘述吧:
/* 悬浮窗遮罩层 */
.drawer-wrapper{
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
visibility: hidden;
opacity: 0;
backdrop-filter: blur(0px);
z-index: 199;
}
如上,实现了一个半模糊的遮罩层;我们通过控制类名的方式实现不同的样式展示,所以需要写不同状态样式,大致如下:
/* 悬浮窗内容触发 */
.drawer-wrapper.open{
visibility: visible;
opacity: 1;
backdrop-filter: blur(10px);
}
/* 悬浮窗内容关闭 */
.drawer-wrapper.close{
visibility: hidden;
opacity: 0;
}
如上,定义了悬浮窗打开、关闭状态下的样式代码,遮罩层部分完成;
弹窗样式和遮罩层逻辑几乎相同,通过控制类名方式控制展示样式,代码如下:
/* 悬浮窗内容*/
.drawer-wrapper_content{
position: absolute;
bottom: 0;
right: 0;
width: 35%;
height: 100%;
overflow: hidden;
background: #f5f5f7;
transition: .3s all;
transform: translate(100%,0);
box-shadow: 0 0 10px rgba(0,0,0,.2);
}
/* 悬浮窗内容触发 */
.drawer-wrapper.open .drawer-wrapper_content{
transform: translate(0,0);
backdrop-filter: blur(0px);
}
/* 悬浮窗内容关闭 */
.drawer-wrapper.close .drawer-wrapper_content{
transform: translate(100%,0);
}
这样,一个悬浮窗的所有状态类名就编辑完毕了;接下来需要做的是不同屏幕宽度下的展示样式(可以不做),代码如下:
/* 悬浮窗内容适配pad / 手机端 */
@media screen and (max-width: 1340px) {
.drawer-wrapper_content{
width: 50%;
}
}
@media screen and (max-width: 960px) {
.drawer-wrapper_content{
width: 60%;
}
}
@media screen and (max-width: 600px) {
.drawer-wrapper_content{
width: 100%;
height: 50%;/* 手机端只展示半屏 */
bottom: 0;
overflow-y: scroll;
}
}
至此,所有的样式定义全部结束。
二、功能逻辑
逻辑代码分为jQuery实现和原生JavaScript实现,但是逻辑是相同的:通过添加和移除类名,使用css的动画属性平滑更改页面的样式,代码如下:
jQuery实现:
$(".Suspended_window_box").click(function () {
if ($(".Suspended_window_box").hasClass("Suspended_window_box")) {
$(".drawer-wrapper").removeClass('close').addClass("open");
$(".Suspended_window_box").removeClass("Suspended_window_box").addClass("toclick");
}
});
$(".drawer-wrapper").click(function () {
if ($(".drawer-wrapper").hasClass("open")) {
$(".toclick").removeClass("toclick").addClass("Suspended_window_box");
$(".drawer-wrapper").removeClass("open").addClass("close");
}
});
原生JavaScript实现:
const window_btn = document.querySelector(".Suspended_window_box");
const derwerDom = document.querySelector(".drawer-wrapper");
window_btn.onclick = function () {
if (window_btn.className == "Suspended_window_box") {
derwerDom.className = "drawer-wrapper open";
window_btn.className = "toclick";
}
};
derwerDom.onclick = function () {
if (derwerDom.className == "drawer-wrapper open") {
derwerDom.className = "drawer-wrapper close";
window_btn.className = "Suspended_window_box";
}
};
如上,在我们点击按钮时,通过判断按钮是否拥有Suspended_window_box 这个类名,从而移除和添加相应的类名;在遮罩层被点击时,我们通过判断是否拥有open这个类名,实现类名的控制。
最后,源码奉上,如有需要可自行获取,感谢您的观看!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>悬浮窗</title>
</head>
<body>
<div class="Suspended_window_box">
<img src="../image/map_image/icon.png" alt="" />
<p>联系我们</p>
</div>
<div class="drawer-wrapper">
<div class="drawer-wrapper_content">
<img src="../../5AB6173D69CA4D58D275DF22CE8D1B19.jpg" alt="" />
<br />
<span>池泽123的悬浮窗</span>
</div>
</div>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
img {
vertical-align: middle;
width: 100%;
}
#app {
background-color: #f5f5f7;
width: 100%;
height: 100vh;
overflow-x: hidden;
}
a {
text-decoration: none;
color: #fff;
}
/* 悬浮窗触发按钮样式 */
.Suspended_window_box{
width: 150px;
height: 70px;
border-radius: 50px 0 0 50px;
overflow: hidden;
background-color: #ebebeb;
position: fixed;
bottom: 100px;
right: -70px;
transition: .3s all; /* 动画运行时间 */
z-index: 198;
}
/* 悬浮窗按钮图片 */
.Suspended_window_box img{
width: auto;
height: 80%;
margin-top: 7px;
margin-left: 7px;
transition: .3s all;
float: left;
}
/* 悬浮窗按钮文字 */
.Suspended_window_box p{
opacity: 0;
line-height: 70px;
float: right;
font-size: 20px;
font-weight: bold;
transition: .3s all;
}
/* 悬浮窗按钮触发 */
.Suspended_window_box:hover{
cursor: pointer;
right: -0px;
background: #ccc;
}
.Suspended_window_box:hover p{
opacity: 1;
}
/* 悬浮窗按钮点击后类名 */
.toclick{
opacity: 0;
}
/* 悬浮窗遮罩层 */
.drawer-wrapper{
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
visibility: hidden;
opacity: 0;
backdrop-filter: blur(0px);
z-index: 199;
}
/* 悬浮窗内容触发 */
.drawer-wrapper.open{
visibility: visible;
opacity: 1;
backdrop-filter: blur(10px);
}
/* 悬浮窗内容关闭 */
.drawer-wrapper.close{
visibility: hidden;
opacity: 0;
}
/* 悬浮窗内容*/
.drawer-wrapper_content{
position: absolute;
bottom: 0;
right: 0;
width: 35%;
height: 100%;
overflow: hidden;
background: #f5f5f7;
transition: .3s all;
transform: translate(100%,0);
box-shadow: 0 0 10px rgba(0,0,0,.2);
}
/* 悬浮窗内容触发 */
.drawer-wrapper.open .drawer-wrapper_content{
transform: translate(0,0);
backdrop-filter: blur(0px);
}
/* 悬浮窗内容关闭 */
.drawer-wrapper.close .drawer-wrapper_content{
transform: translate(100%,0);
}
/* 悬浮窗内容适配pad / 手机端 */
@media screen and (max-width: 1340px) {
.drawer-wrapper_content{
width: 50%;
}
}
@media screen and (max-width: 960px) {
.drawer-wrapper_content{
width: 60%;
}
}
@media screen and (max-width: 600px) {
.drawer-wrapper_content{
width: 100%;
height: 50%;/* 手机端只展示半屏 */
bottom: 0;
overflow-y: scroll;
}
}
/* 悬浮窗内部样式 可自定义 */
.drawer-wrapper_content img{
width: 80%;
display: block;
margin: 0 auto;
margin-top: 20px;
}
.drawer-wrapper_content span{
display: block;
width: 100%;
text-align: center;
line-height: 40px;
font-size: 20px;
}
</style>
<script src="../公共样式/jq.js"></script>
<script>
// jquery
$(".Suspended_window_box").click(function () {
if ($(".Suspended_window_box").hasClass("Suspended_window_box")) {
$(".drawer-wrapper").removeClass('close').addClass("open");
$(".Suspended_window_box").removeClass("Suspended_window_box").addClass("toclick");
}
});
$(".drawer-wrapper").click(function () {
if ($(".drawer-wrapper").hasClass("open")) {
$(".toclick").removeClass("toclick").addClass("Suspended_window_box");
$(".drawer-wrapper").removeClass("open").addClass("close");
}
});
// 原生
const window_btn = document.querySelector(".Suspended_window_box");
const derwerDom = document.querySelector(".drawer-wrapper");
window_btn.onclick = function () {
if (window_btn.className == "Suspended_window_box") {
derwerDom.className = "drawer-wrapper open";
window_btn.className = "toclick";
}
};
derwerDom.onclick = function () {
if (derwerDom.className == "drawer-wrapper open") {
derwerDom.className = "drawer-wrapper close";
window_btn.className = "Suspended_window_box";
}
};
</script>
</body>
</html>

206

被折叠的 条评论
为什么被折叠?



