单击任何其他元素时,Javascript 更改 css

标签 javascript jquery

现在我有一个固定在屏幕底部的菜单,高度为 35 像素,单击时它会执行以下操作:

Javascript 代码

    function slideChat() {
    div = document.getElementById('chat-slide');
    div.style.height = "100px";
    div.style.transition = "height .4s ease";
    }

HTML 代码

 <div id="chat-slide" class="mobilechaticonON">
      <button onclick="javascript:slideChat();" class="mobilechat"><span></span></button>
      <br>
      <button class="smschat"><a href="bing.com"><span></span></a></button>
      <br>
      <button class="webchat"><a href="google.com"><span></span></a></button>
    </div>

CSS

.mobilechaticonON {
  display: block;
  height: 35px;
  position: fixed;
  bottom: 0;
  left: 21%;
  width: auto;
  padding: 5px 15px;
  background: #16a085;
  border-radius: 4px 4px 0px 0px;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.6);
  z-index: 999;
  transition: height .4s ease;
 }
 .smschat span:before {
     content:"Chat through text message";
 }
 .webchat span:before {
     content:"Chat using web chat";
 }
 .mobilechat span:before {
     content:"Live Sales Chat - Online!";
 }
 .mobilechaticonON button {
     border: 0;
     background: transparent;
     height: 35px;
 }
 .mobilechaticonON button span:before {
     font-family: Arial;
     font-size: 1.2em;
     color: #fff;
 }

我正在尝试找出一种方法,当用户点击此 div 之外的其他内容时,div 会返回到 35px 而不是 100px。到目前为止没有任何运气。

Here is a fiddle但我不确定为什么初始幻灯片功能甚至在那里都不起作用。在我的普通页面上工作正常。哦

最佳答案

定位文档,如果点击来自chat-slide之外的任何元素,则将高度改回

$(document).on('click', function(e) {
    console.log('clicking anywhere works !');
    if ( ! $(e.target).closest('#chat-slide').length ) {
        $('#chat-slide').css('height', '35px');
    }
});

如果您使用的是 jQuery(您使用 jQuery 标记它),请使用适当的事件处理程序

$('.mobilechat').on('click', function() {
    console.log('clicking the button works !');
    $('#chat-slide').css({
        height: '100px',
        transition : 'height .4s ease' 
    });
});

同时改变

<button onclick="javascript:slideChat();" class="mobilechat">

只是

<button class="mobilechat">

关于单击任何其他元素时,Javascript 更改 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22103612/

相关文章:

javascript - jquery 查找并将 < 替换为 ">"

jquery - 使用 jquery 最简单的拖放文件 uploader 插件

jquery - 如何从变量获取 slider 最小值、最大值。

javascript - HTML CSS - 不可拖动的图像和整洁的代码

javascript - jQuery UI : Best way to create two drag/drop lists where one is a Sortable, 其他办法就掉了吗?

javascript - 如何在 Vue.JS 模板中循环时每 2 个元素添加 <div class ="row">?

javascript - 如何知道 nsIWebBrowserPersist 下载何时完成? (火狐 30、Javascript)

javascript - 想要将 AJAX 时事通讯注册跟踪为 Google Analytics 事件

javascript - 如何修复 Redux-React 应用程序中的这些错误?

javascript - 从 JSON api 解析图像 url