jQuery实现的网页右下角tab样式在线客服效果代码

jQuery实现的网页右下角tab样式在线客服效果,可以让网站提供在线客服支持,方便用户与客服人员的交流。以下是实现步骤及代码示例:

1. 创建HTML结构

在页面中需要创建以下HTML结构:

<div class="chat-wrapper">
    <div class="chat-header">
        <i class="fa fa-comments" aria-hidden="true"></i>
        <span>客服在线</span>
        <i class="fa fa-angle-down" aria-hidden="true"></i>
    </div>
    <div class="chat-body">
        <div class="chat-message"></div>
        <textarea class="chat-input" placeholder="请输入消息"></textarea>
    </div>
</div>
<div class="chat-tab">在线客服</div>

chat-wrapper 是整个客服窗口的容器,包含头部和主体区域;chat-header 是头部区域,包含图标、标题和收起展开箭头;chat-body 是主体区域,包含消息展示区和输入框;chat-tab 是在线客服的标签,位于页面的右下角。

2. 创建CSS样式

为了让客服窗口和标签看起来更美观,需要为它们创建相应的CSS样式:

.chat-wrapper {
    width: 300px;
    height: 400px;
    border: 1px solid #ccc;
    position: fixed;
    bottom: 0;
    right: 0;
    z-index: 999;
    border-radius: 10px 0 0 0;
    overflow: hidden;
    transition: all 0.5s ease;
}

.chat-header {
    background-color: #556b8d;
    color: #fff;
    height: 40px;
    line-height: 40px;
    padding: 0 10px;
    cursor: pointer;
}

.chat-header span {
    margin-left: 10px;
}

.chat-body {
    height: 320px;
    overflow-y: auto;
    padding: 10px;
}

.chat-message {
    margin-bottom: 10px;
}

.chat-input {
    width: 100%;
    height: 80px;
    outline: none;
}

.chat-tab {
    width: 80px;
    height: 30px;
    background-color: #556b8d;
    color: #fff;
    font-size: 14px;
    text-align: center;
    line-height: 30px;
    position: fixed;
    bottom: 20px;
    right: 20px;
    cursor: pointer;
    z-index: 999;
    border-radius: 15px 0 0 0;
}

3. 编写jQuery代码

接下来需要使用jQuery来实现在线客服的交互效果:

$(function () {
    // 将聊天框收缩,只显示标签
    $(".chat-wrapper").hide();
    $(".chat-tab").click(function () {
        $(".chat-wrapper").show();
    });

    // 收起聊天框
    $(".chat-header i.fa-angle-down").click(function () {
        $(".chat-wrapper").animate({
            height: "40px"
        });
        $(".chat-body").hide();
        $(this).hide();
        $(".chat-header i.fa-angle-up").show();
    });

    // 展开聊天框
    $(".chat-header i.fa-angle-up").click(function () {
        $(".chat-wrapper").animate({
            height: "400px"
        });
        $(".chat-body").show();
        $(this).hide();
        $(".chat-header i.fa-angle-down").show();
    });

    // 发送消息
    $(".chat-input").keypress(function (event) {
        if (event.keyCode == 13) {
            var message = $(this).val();
            $(this).val("");
            $(".chat-message").append("<p>我:" + message + "</p>");
        }
    });
});

首先将聊天窗口隐藏,只显示标签;当用户点击标签时,聊天窗口展开。当用户点击收起箭头时,聊天窗口收起,点击展开箭头时,聊天窗口展开。当用户在输入框中按下回车键时,我们会获取输入框内容,显示到聊天框中。

示例

以下是示例代码,在线演示请参考以下链接:

https://codepen.io/hezhengjie/pen/RwejKRj

<!DOCTYPE html>
<html>
<head>
    <title>jQuery实现的网页右下角tab样式在线客服效果</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style>
        .chat-wrapper {
            width: 300px;
            height: 400px;
            border: 1px solid #ccc;
            position: fixed;
            bottom: 0;
            right: 0;
            z-index: 999;
            border-radius: 10px 0 0 0;
            overflow: hidden;
            transition: all 0.5s ease;
        }

        .chat-header {
            background-color: #556b8d;
            color: #fff;
            height: 40px;
            line-height: 40px;
            padding: 0 10px;
            cursor: pointer;
        }

        .chat-header span {
            margin-left: 10px;
        }

        .chat-body {
            height: 320px;
            overflow-y: auto;
            padding: 10px;
        }

        .chat-message {
            margin-bottom: 10px;
        }

        .chat-input {
            width: 100%;
            height: 80px;
            outline: none;
        }

        .chat-tab {
            width: 80px;
            height: 30px;
            background-color: #556b8d;
            color: #fff;
            font-size: 14px;
            text-align: center;
            line-height: 30px;
            position: fixed;
            bottom: 20px;
            right: 20px;
            cursor: pointer;
            z-index: 999;
            border-radius: 15px 0 0 0;
        }
    </style>
</head>
<body>
<div class="chat-wrapper">
    <div class="chat-header">
        <i class="fa fa-comments" aria-hidden="true"></i>
        <span>客服在线</span>
        <i class="fa fa-angle-down" aria-hidden="true"></i>
        <i class="fa fa-angle-up" aria-hidden="true"></i>
    </div>
    <div class="chat-body">
        <div class="chat-message"></div>
        <textarea class="chat-input" placeholder="请输入消息"></textarea>
    </div>
</div>
<div class="chat-tab">在线客服</div>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
    $(function () {
        // 将聊天框收缩,只显示标签
        $(".chat-wrapper").hide();
        $(".chat-tab").click(function () {
            $(".chat-wrapper").show();
        });

        // 收起聊天框
        $(".chat-header i.fa-angle-down").click(function () {
            $(".chat-wrapper").animate({
                height: "40px"
            });
            $(".chat-body").hide();
            $(this).hide();
            $(".chat-header i.fa-angle-up").show();
        });

        // 展开聊天框
        $(".chat-header i.fa-angle-up").click(function () {
            $(".chat-wrapper").animate({
                height: "400px"
            });
            $(".chat-body").show();
            $(this).hide();
            $(".chat-header i.fa-angle-down").show();
        });

        // 发送消息
        $(".chat-input").keypress(function (event) {
            if (event.keyCode == 13) {
                var message = $(this).val();
                $(this).val("");
                $(".chat-message").append("<p>我:" + message + "</p>");
            }
        });
    });
</script>
</body>
</html>

在网页中引入jQuery和font-awesome样式,即可实现在线客服效果。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:jQuery实现的网页右下角tab样式在线客服效果代码 - Python技术站

(0)
上一篇 2023年5月28日
下一篇 2023年5月28日

相关文章

  • jqPlot Option配置对象详解

    jqPlot Option配置对象详解 什么是 jqPlot Option 配置对象? jqPlot 是一个流行的基于 jQuery 的开源图表库,它提供了各种功能和选项来创建多种类型的图表。jqPlot 的大多数功能和样式都可通过选项进行定制,而这些选项是通过一个特定的配置对象来传递的,这就是 jqPlot Option 配置对象。 jqPlot Opti…

    jquery 2023年5月28日
    00
  • jQWidgets jqxListBox disableAt()方法

    jQWidgets jqxListBox disableAt()方法详解 jQWidgets是一个基于jQuery的UI组件库,提供了丰富UI组件工具包。jqxListBox是其中之一,本文将详细介绍jqxListBox的disableAt()方法,包括定义、语法和示例。 disableAt()方法的定义 jqxListBox的disableAt()方法用于…

    jquery 2023年5月10日
    00
  • 如何在jQuery中触发窗口尺寸调整事件

    首先,我们需要了解的是,jQuery中触发窗口尺寸调整事件的方式主要有两种,分别是: 使用jQuery的resize()方法来监听窗口尺寸的变化。 使用原生的JavaScript方式监听窗口尺寸的变化,然后再通过jQuery触发事件。 下面,我们将分别详细介绍这两种方式的使用方法。 方式一:使用jQuery的resize()方法监听尺寸变化 首先,我们需要使…

    jquery 2023年5月12日
    00
  • jQuery中on()方法用法实例详解

    jQuery中on()方法用法实例详解 简介 jQuery是一个流行的JavaScript库,为开发人员提供了一种更为简单、高效的编写与操作JavaScript的方式。当需要为网站添加交互功能时,jQuery是非常实用的选择之一。 其中,on()是jQuery中众多事件处理方法之一,用于在绑定事件时为全局和未来元素绑定事件。 语法 $(selector).o…

    jquery 2023年5月27日
    00
  • jQWidgets jqxListBox addItem()方法

    jQWidgets jqxListBox addItem()方法攻略 简介 jQWidgets 是一个基于 jQuery 的 UI 组件库,提供了丰富的 UI 组件和工具,可于创建代化应程序。jqxListBox 组件是一个用于显示列表的组件,支持多选和单选。本攻略将详细介绍 jqxListBox 的 addItem() 方法,包括如何使用和示例说明。 使用…

    jquery 2023年5月10日
    00
  • 在JS/jQuery中触发一个按键/下键/上键事件

    当需要在JS/jQuery中模拟按键/下键/上键事件时,可以使用trigger()方法来触发这些事件。下面是详细的攻略: 触发按键事件 以下是一个示例,演示如何使用trigger()方法触发按键事件: <!DOCTYPE html> <html> <head> <title>Trigger Key Press …

    jquery 2023年5月9日
    00
  • Chrome插件(扩展)开发全攻略(完整demo)

    这里是关于「Chrome插件(扩展)开发全攻略(完整demo)」的详细讲解。 什么是Chrome插件 Chrome插件是一种运行在Chrome浏览器上的扩展程序,可以为浏览器增加一些非常实用的功能,例如广告屏蔽、密码管理、网页截屏、翻译等等。开发者可以使用HTML、CSS、JavaScript等前端技术开发Chrome插件,甚至还可以与浏览器和其他插件进行交…

    jquery 2023年5月18日
    00
  • jQWidgets jqxMaskedInput promptChar属性

    jQWidgets jqxMaskedInput promptChar属性详解 jQWidgets是一个基于jQuery的UI组件库,提供了丰富UI组件工具包。jqxMaskedInput是其中之一。本文将详细介绍jqxMaskedInput的promptChar属性,包括用法、语法和示例。 promptChar属性的语法 jqxMaskedInput的pr…

    jquery 2023年5月10日
    00
合作推广
合作推广
分享本页
返回顶部