容器元素的滚动条回到顶部

关闭再打开后,容器元素的滚动条回到顶部

解决方法:

1、通过打开开发者工具(F12),找到滚动条所属元素为 el-textarea__inner,其父类 class="el-textarea content"

2、代码,通过元素的方法 scrollTo(0, 0) 让滚动条回到顶部

TypeScript 复制代码
<script setup lang="ts" name="BaseShowContentDialog">
/**
 * 显示内容模态框组件
 */
defineOptions({ name: "BaseShowContentDialog" });
import { nextTick, ref } from "vue";

interface Props {
  /** 标题 */
  title?: string;
  /** 内容 */
  content?: string;
}
const props = withDefaults(defineProps<Props>(), { title: "", content: "" });
// 对话框显示标识
const dialogVisible = ref(false);

// 显示对话框
const showDialog = async () => {
  dialogVisible.value = true;

  // 滚动条回到顶部,通过开发者工具追查到滚动条对应的组件元素是 el-input,以应的原始元素是 textarea,其子类 class="el-textarea__inner"
  // 等待 DOM 渲染完毕
  await nextTick();
  // 指定元素(.content .el-textarea__inner,其中 .content 是指定的父类类名,.el-textarea__inner 是子类类名)的滚动条滚动到顶部
  (document.querySelector(".content .el-textarea__inner") as HTMLElement)?.scrollTo(0, 0);
};

// 关闭对话框
const closeDialog = () => {
  dialogVisible.value = false;
};

defineExpose({ showDialog });
</script>

<template>
  <div>
    <el-dialog
      :title="props.title"
      width="800px"
      top="0vh"
      style="border-radius: 10px"
      center
      v-model="dialogVisible"
      :close-on-press-escape="true"
      :close-on-click-modal="false"
      :show-close="false"
      draggable
      @closed="closeDialog">
      <template #>
        <el-input class="content" type="textarea" v-model="props.content" rows="26" readonly />
      </template>
      <template #footer>
        <div>
          <el-button type="primary" @click="closeDialog">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.content {
  // white-space: pre-wrap的作用是保留空格,并且除了碰到源码中的换行和会换行外,还会自适应容器的边界进行换行。
  white-space: pre-wrap;
}
</style>
相关推荐
海鸥两三1 天前
uniapp 小程序引入 uview plus 框架,获得精美的UI框架
前端·vue.js·ui·小程序·uni-app
lightgis1 天前
16openlayers加载COG(云优化Geotiff)
前端·javascript·html·html5
小飞大王6661 天前
TypeScript核心类型系统完全指南
前端·javascript·typescript
你的人类朋友1 天前
✍️记录自己的git分支管理实践
前端·git·后端
合作小小程序员小小店1 天前
web网页开发,在线考勤管理系统,基于Idea,html,css,vue,java,springboot,mysql
java·前端·vue.js·后端·intellij-idea·springboot
防火墙在线1 天前
前后端通信加解密(Web Crypto API )
前端·vue.js·网络协议·node.js·express
Jacky-0081 天前
Node + vite + React 创建项目
前端·react.js·前端框架
CoderYanger1 天前
前端基础——CSS练习项目:百度热榜实现
开发语言·前端·css·百度·html·1024程序员节
i_am_a_div_日积月累_1 天前
10个css更新
前端·css
她是太阳,好耀眼i1 天前
Nvm 实现vue版本切换
javascript·vue.js·ecmascript