• vue之下拉菜单


    下拉菜单主要运用了hover显示与隐藏以及定位的问题

    效果图:
    在这里插入图片描述

    可能出现的问题

    定位后菜单的div无法自适应(宽度由定位的父元素决定导致换行)

    div {
    	white-space: nowrap;
    	width: auto;
    }
    
    • 1
    • 2
    • 3
    • 4

    制作下拉菜单时,a标签列表根据最大的内容自适应宽度撑满盒子

    a {
    	display: block;
    	width: 100%;
    }
    
    • 1
    • 2
    • 3
    • 4

    CSS下拉菜单鼠标移入菜单内容时,菜单隐藏消失的问题

    首先,我们用 css 写下拉菜单,一般过程是这样:一个大的父元素框架—— drop-list ,包含一个触发下拉菜单的起点—— drop-text,和一个菜单内容——drop-content,鼠标移动到父元素上时,下拉菜单显示,鼠标移开时,菜单内容隐藏。

    按照道理来说,鼠标放置父元素整体范围是不会收起菜单内容的,经过排错后发现,drop-content与 drop-text 存在空隙,鼠标移动到那个缝隙时,就离开了父元素,所以会存在鼠标移动时,菜单内容收起的情况。

    解决方法:

    1. 让空隙消失,设置 droptext 的下外边距为0即 margin-bottom:0; drop-content 的上外边距为0 即 margin-top:0
    2. 若需要间隙可以在drop-content内包裹一层在里面设置间隙,把drop-content透明隐藏。

    代码如下:

    <template>
      <div class="container">
        <div class="dropdown">
          
          <span class="dropdown-title"> 下拉菜单 span>
          
          <div class="dropdown-content">
            <div class="dropdown-menu">
              <div class="menuItem">菜单1div>
              <div class="menuItem">菜单菜单1div>
              <div class="menuItem">菜单2div>
              <div class="menuItem">菜单菜单菜单1div>
              <div class="menuItem">菜单3div>
            div>
          div>
        div>
      div>
    template>
    
    <script>
    export default {
      data() {
        return {};
      },
      mounted() {},
      methods: {},
    };
    script>
    
    <style lang="scss" scoped>
    .container {
      padding: 20px;
    }
    
    .dropdown {
      margin: 0 20px;
      .dropdown-title {
        display: inline-block;
        position: relative;
        height: 60px;
        line-height: 60px;
        font-size: 20px;
        color: #fff;
        background-color: #000;
        padding: 0 24px;
        border-radius: 4px;
        cursor: pointer;
        // transition: all 1s ease-in-out;
      }
    
      .dropdown-content {
        // 定位显示局域
        position: absolute;
        visibility: hidden; // 隐藏
        opacity: 0; // 隐藏
        transition: all 0.6s ease-in-out;
    
        .dropdown-menu {
          margin-top: 4px;  // 与title制造距离
          padding: 10px 8px 15px; // 给下面多留一点距离,抵消视觉差
          color: white;
          background-color: rgba($color: #000, $alpha: 0.8);
          border-radius: 4px;
          .menuItem {
            width: 100%;  // 自适应宽度
            white-space: nowrap;
            padding: 10px 16px;
            font-size: 16px;
            color: white;
            cursor: pointer;
            border-radius: 4px;
            &:hover {
              background-color: #ccc;
            }
          }
        }
      }
    
      &:hover .dropdown-content {
        visibility: visible;
        opacity: 1;
      }
    }
    style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
  • 相关阅读:
    7年坚定投身“高清头显”,纳德光学聚焦打造高清头显领导品牌
    这c代码,会让你发疯
    调试记录 RTC芯片 型号INS5902A 断电之后再开机系统时间不变的问题。
    C# 连接SQL Sever 数据库
    Kubernetes 的有状态和无状态服务
    SpringBoot @PropertySource注解使用
    Java中数组array和列表list相互转换
    目标检测算法——自动驾驶开源数据集汇总2(附下载链接)
    【无标题】
    《每日一题》NO.43:如何使用CG(clock gating) cell?
  • 原文地址:https://blog.csdn.net/wgh4318/article/details/126171345