滚动视图解决ElementUI NavMenu 导航菜单过长显示的问题

menu
注册组件

<script>
import {
  addResizeListener,
  removeResizeListener
} from 'element-ui/src/utils/resize-event'

export default {
  props: {
    width: {
      type: String,
      default: '100%'
    }
  },

  data() {
    return {
      scrollable: false,
      navOffset: 30
    }
  },

  computed: {
    navStyle() {
      return {
        transform: `translateX(-${this.navOffset}px)`
      }
    }
  },

  updated() {
    this.update()
  },

  mounted() {
    addResizeListener(this.$el, this.update)
  },

  beforeDestroy() {
    if (this.$el && this.update) removeResizeListener(this.$el, this.update)
  },

  methods: {
    scrollPrev() {
      const containerSize = this.$refs.navScroll.offsetWidth
      const currentOffset = this.navOffset

      if (!currentOffset) return

      const newOffset =
        currentOffset > containerSize ? currentOffset - containerSize : 0

      this.navOffset = newOffset
    },
    scrollNext() {
      const navSize = this.$refs.nav.offsetWidth
      const containerSize = this.$refs.navScroll.offsetWidth
      const currentOffset = this.navOffset

      if (navSize - currentOffset <= containerSize) return

      const newOffset =
        navSize - currentOffset > containerSize * 2
          ? currentOffset + containerSize
          : navSize - containerSize

      this.navOffset = newOffset
    },
    scrollToActiveTab() {
      if (!this.scrollable) return

      const nav = this.$refs.nav
      const activeTab = this.$el.querySelector('.is-active')
      if (!activeTab) return
      const navScroll = this.$refs.navScroll
      const activeTabBounding = activeTab.getBoundingClientRect()
      const navScrollBounding = navScroll.getBoundingClientRect()
      const maxOffset = nav.offsetWidth - navScrollBounding.width
      const currentOffset = this.navOffset
      let newOffset = currentOffset

      if (activeTabBounding.left < navScrollBounding.left) {
        newOffset =
          currentOffset - (navScrollBounding.left - activeTabBounding.left)
      }
      if (activeTabBounding.right > navScrollBounding.right) {
        newOffset =
          currentOffset + activeTabBounding.right - navScrollBounding.right
      }
      newOffset = Math.max(newOffset, 0)
      this.navOffset = Math.min(newOffset, maxOffset)
    },
    update() {
      if (!this.$refs.nav) return
      const navSize = this.$refs.nav.offsetWidth
      this.height = this.$refs.nav.offsetHeight
      const containerSize = this.$refs.navScroll.offsetWidth
      const currentOffset = this.navOffset
      if (containerSize < navSize) {
        const currentOffset = this.navOffset
        this.scrollable = this.scrollable || {}
        this.scrollable.prev = currentOffset
        this.scrollable.next = currentOffset + containerSize < navSize
        if (navSize - currentOffset < containerSize) {
          this.navOffset = navSize - containerSize
        }
      } else {
        this.scrollable = false
        if (currentOffset > 0) {
          this.navOffset = 0
        }
      }
    }
  },

  render() {
    const { navStyle, scrollable, scrollNext, scrollPrev, height, width } = this
    const lineHeight = {
      'line-height': height + 'px'
    }
    const scrollBtn = scrollable
      ? [
        <span
          class={['scrollView__nav-prev', scrollable.prev ? '' : 'is-disabled']}
          on-click={scrollPrev}
        >
          <i
            style={lineHeight}
            class='el-icon-arrow-left'></i>
        </span>,
        <span
          class={['scrollView__nav-next', scrollable.next ? '' : 'is-disabled']}
          on-click={scrollNext}
        >
          <i style={lineHeight}
            class='el-icon-arrow-right'></i>
        </span>
      ]
      : null

    return (
      <div
        class={[
          'scrollView__nav-wrap',
          scrollable ? 'is-scrollable' : ''
        ]}
        style={{ width }}
      >
        {scrollBtn}
        <div
          class='scrollView__nav-scroll'
          ref='navScroll'
        >
          <div
            class='scrollView__nav'
            ref='nav'
            style={navStyle}
          >
            {this.$slots.default}
          </div>
        </div>
      </div>
    )
  }
}
</script>

<style lang="less">
.scrollView__nav-wrap {
  display: inline-block;
  overflow: hidden;
  margin-bottom: -1px;
  position: relative;
  vertical-align: middle;
}

.scrollView__nav-wrap.is-scrollable {
  padding: 0 25px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

.scrollView__nav-wrap::after {
  display: none;
}

.scrollView__nav-scroll {
  overflow: hidden;
}

.scrollView__nav {
  white-space: nowrap;
  position: relative;
  transition: transform 0.3s, -webkit-transform 0.3s;
  float: left;
  z-index: 2;
  margin-left: -62px;
}

.scrollView__nav-prev {
  left: 0;
}
.scrollView__nav-next {
  right: 0;
}
.scrollView__nav-next,
.scrollView__nav-prev {
  position: absolute;
  cursor: pointer;
  line-height: 32px;
  font-size: 12px;
  color: #909399;
}
</style>

全部复制

把内容包裹起来

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
对于展示海量数据,传统的方式是将数据全部加载到页面中,这样会导致页面加载缓慢,甚至出现卡顿的情况。而虚拟滚动视图可以只加载当前可见的数据,随着滚动条的滚动,动态加载新的数据,从而提高页面的加载速度和用户体验。 在使用 Easyui 进行数据滚动展现时,可以使用其提供的 datagrid 组件,并设置其属性 virtual scrolling 为 true。这样就可以启用虚拟滚动视图功能。 具体操作如下: 1. 引入 Easyui 的 CSS 和 JS 文件。 2. 在 HTML 中添加一个 div 元素,并设置其 id。 3. 在 JS 中使用 datagrid 组件,并设置其 virtual scrolling 属性为 true。 代码示例: HTML: ```html <div id="datagrid"></div> ``` JS: ```javascript $('#datagrid').datagrid({ url: 'get_data.php', //获取数据的接口 pagination: false, //关闭分页功能 remoteFilter: true, //启用远程过滤 virtual scrolling: true, //启用虚拟滚动视图 rownumbers: true, //显示行号 singleSelect: true, //只允许选择一行 columns:[[ {field:'name',title:'姓名',width:100}, {field:'age',title:'年龄',width:100}, {field:'gender',title:'性别',width:100}, {field:'address',title:'地址',width:200} ]] }); ``` 4. 在后端编写相应的接口,用于获取数据。在接口中需要根据 datagrid 组件传递的参数进行数据筛选和分页处理。 PHP 代码示例: ```php <?php $page = isset($_POST['page']) ? intval($_POST['page']) : 1; //获取当前页码 $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10; //获取每页显示的数据量 $offset = ($page - 1) * $rows; //计算偏移量 //从数据库中获取数据 $sql = "SELECT * FROM table LIMIT $offset, $rows"; $result = mysql_query($sql); $data = array(); while($row = mysql_fetch_assoc($result)){ $data[] = $row; } //返回数据 echo json_encode(array( 'total' => count($data), //总数据量 'rows' => $data //当前页的数据 )); ?> ``` 通过以上步骤,就可以实现 Easyui 的虚拟滚动视图功能,用于展示海量数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值