js之 事件监听(鼠标、焦点、键盘、文本)

目标:能够给DOM元素添加事件监听
什么是事件:事件是在编程时系统内发生的动作或者发生的事情,比如用户在网页上单击一个按钮
什么是事件监听
       就是让程序检测是否有事件产生,一旦有事件触发,就立即调用一个函数做出响应,也称为 绑定事件或者注册事件比如鼠标经过显示下拉菜单,比如点击可以播放轮播图等等

1、鼠标事件

鼠标触发:

click 鼠标点击
mouseenter 鼠标经过

mouseleave 鼠标离开

简单案例需求:关闭广告 

<style>
    .box {
        width: 400px;
        height: 200px;
        margin: auto;
        background: skyblue;
        position: relative;
        font-size: 50px;
        text-align: center;
        line-height: 200px;
    }

    .choose {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        background: green;
        position: absolute;
        top: 10px;
        right: 10px;
        cursor: pointer;
    }

    .choose:hover {
        background: #b8b0b0;
    }
</style>
<body>
    <div class="box">
        广告
        <button class="choose">X</button>
    </div>
    <script>
        const box = document.querySelector('.box')
        const choose = document.querySelector('.choose')
        choose.addEventListener('click', function () {
            box.style.display = 'none'
        })
    </script>
</body>

2、焦点事件

表单获得光标:
获得焦点focus
失去焦点blur

3、键盘事件

键盘触发:
Keydown 键盘按下触发
Keyup键盘抬起触发

4、文本事件

表单输入触发:
input 用户输入事件

 5、案例:

Tab 切换,页面效果:

 <style>
    * {
      margin: 0;
      padding: 0;
    }

    .tab {
      width: 590px;
      height: 340px;
      margin: 20px;
      border: 1px solid #e4e4e4;
    }

    .tab-nav {
      width: 100%;
      height: 60px;
      line-height: 60px;
      display: flex;
      justify-content: space-between;
    }

    .tab-nav h3 {
      font-size: 24px;
      font-weight: normal;
      margin-left: 20px;
    }

    .tab-nav ul {
      list-style: none;
      display: flex;
      justify-content: flex-end;
    }

    .tab-nav ul li {
      margin: 0 20px;
      font-size: 14px;
    }

    .tab-nav ul li a {
      text-decoration: none;
      border-bottom: 2px solid transparent;
      color: #333;
    }

    .tab-nav ul li a.active {
      border-color: #e1251b;
      color: #e1251b;
    }

    .tab-content {
      padding: 0 16px;
    }

    .tab-content .item {
      display: none;
    }
    img {
      width: 100%;
      height: 250px;
    }

    .tab-content .item.active {
      display: block;
    }
  </style>
</head>

<body>
  <div class="tab">
    <div class="tab-nav">
      <h3>每日新品</h3>
      <ul>
        <li><a class="active" href="#">精选</a></li>
        <li><a href="#">美食</a></li>
        <li><a href="#">百货</a></li>
        <li><a href="#">医美</a></li>
        <li><a href="#">预告</a></li>
      </ul>
    </div>
    <div class="tab-content">
      <div class="item active"><img src="./images/Mn_1.jpg" alt="" /></div>
      <div class="item"><img src="./images/Mn_2.jpg" alt="" /></div>
      <div class="item"><img src="./images/Mn_3.jpg" alt="" /></div>
      <div class="item"><img src="./images/Mn_4.jpg" alt="" /></div>
      <div class="item"><img src="./images/Mn_5.jpg" alt="" /></div>
    </div>
  </div>
  <script>
    const as = document.querySelectorAll('.tab-nav a')
    for (let i = 0; i < as.length; i++) {
      as[i].addEventListener('mouseenter', function () {
        // 移除类active
        document.querySelector('.tab-nav .active').classList.remove('active')
        //添加类 active 
        this.classList.add('active')
        // 对应 item类 
        document.querySelector('.tab-content .active').classList.remove('active')
        document.querySelector(`.tab-content .item:nth-child(${i + 1})`).classList.add('active')
      })
    }
  </script>
</body>

6、以上综合案例

发布评论,页面效果:

 <style>
    .wrapper {
      min-width: 400px;
      max-width: 800px;
      display: flex;
      justify-content: flex-end;
    }

    .avatar {
      display: flex;
      width: 48px;
      height: 48px;
      border-radius: 50%;
      overflow: hidden;
      background: url(./images/Mn_1.jpg) no-repeat center / cover;
      margin-right: 20px;
    }

    .wrapper textarea {
      outline: none;
      border-color: transparent;
      resize: none;
      background: #f5f5f5;
      border-radius: 4px;
      flex: 1;
      padding: 10px;
      transition: all 0.5s;
      height: 30px;
    }

    .wrapper textarea:focus {
      border-color: #e4e4e4;
      background: #fff;
      height: 50px;
    }

    .wrapper button {
      background: #00aeec;
      height: 50px;
      color: #fff;
      border: none;
      border-radius: 4px;
      margin-left: 10px;
      width: 70px;
      cursor: pointer;
    }

    .wrapper .total {
      margin-right: 80px;
      color: #999;
      margin-top: 5px;
      opacity: 0;
      transition: all 0.5s;
    }

    .list {
      min-width: 400px;
      max-width: 800px;
      display: flex;
    }

    .list .item {
      width: 100%;
      display: flex;
    }

    .list .item .info {
      flex: 1;
      border-bottom: 1px dashed #e4e4e4;
      padding-bottom: 10px;
    }

    .list .item p {
      margin: 0;
    }

    .list .item .name {
      color: #FB7299;
      font-size: 14px;
      font-weight: bold;
      margin-left: 40px;
    }

    .list .item .text {
      color: #333;
      padding: 10px 0;
    }

    .list .item .time {
      color: #999;
      font-size: 12px;
    }
  </style>
</head>

<body>
  <div class="wrapper">
    <i class="avatar"></i>
    <textarea id="tx" placeholder="请输入您的评论" rows="2" maxlength="200"></textarea>
    <button>发布</button>
  </div>
  <div class="wrapper">
    <span class="total">0/200字</span>
  </div>
  <div class="list">
    <div class="item" style="display: none;">
      <i class="avatar"></i>
      <div class="info">
        <p class="name">linsir</p>
        <p class="text"></p>
        <p class="time"></p>
      </div>
    </div>
  </div>
  <script>
    const tx = document.querySelector('#tx')
    const total = document.querySelector('.total')
    const item = document.querySelector('.item')
    const text = document.querySelector('.text')
    const time = document.querySelector('.time')
    const date = new Date()
    // 1. 文本域获得焦点,total 显示
    tx.addEventListener('focus', function () {
      total.style.opacity = 1
    })
    // 2. 文本域失去焦点,total 隐藏
    tx.addEventListener('blur', function () {
      total.style.opacity = 0
    })
    // 3. 检测用户输入字数
    tx.addEventListener('input', function () {
      total.innerHTML = `${tx.value.length}/200字`
    })

    //  4、按下回车键发布评论
    tx.addEventListener('keyup', function (e) {
      if (e.key === 'Enter') {
        // 判断用户输入是否为空  str.trim() 去除字符串左右的空格
        if (tx.value.trim()) {
          item.style.display = 'block'
          text.innerHTML = tx.value
          // date.toLocaleString() 格式化时间
          time.innerHTML = date.toLocaleString()
        }
        tx.value = ''
        total.innerHTML = '0/200字'
      }
    })
  </script>
</body>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linsir 一啵叶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值