当前位置: 首页 > news >正文

scrollIntoView使用与属性详解

scrollIntoView 使用与属性详解

效果图如下图所示
在这里插入图片描述

如果要想让元素滚动到指定位置

window.onload = function () {containerItems[6].scrollIntoView({ behavior: "smooth" });
};

js 代码

const containerItems = document.querySelectorAll(".container div");
const gobackTop = document.querySelector(".gobackTop");window.onload = function () {containerItems[6].scrollIntoView({ behavior: "smooth" });
};gobackTop.addEventListener("click", function () {window.scrollTo(0, 0);
});

html 元素

<div class="container"><div><h1>1</h1></div><div><h1>2</h1></div><div><h1>3</h1></div><div><h1>4</h1></div><div><h1>5</h1></div><div><h1>6</h1></div><div><h1>7</h1></div><div><h1>8</h1></div><div><h1>9</h1></div><div><h1>10</h1></div><div><h1>11</h1></div><div><h1>12</h1></div><div><h1>13</h1></div><div><h1>14</h1></div><div><h1>15</h1></div><div><h1>16</h1></div>
</div><div class="gobackTop">返回顶部</div>

css 样式

html {/* 平滑滚动到顶部 */scroll-behavior: smooth;
}.container {margin: 0 auto;width: 300px;
}.container > div {width: 300px;height: 300px;text-align: center;line-height: 300px;background-color: #efcd11;
}.gobackTop {position: fixed;bottom: 50px;right: 50px;width: 50px;height: 50px;background-color: #efcd11;
}
http://www.lryc.cn/news/194732.html

相关文章:

  • 【LeetCode热题100】--169.多数元素
  • LeetCode 面试题 10.01. 合并排序的数组
  • 揭秘OLED透明拼接屏的参数规格:分辨率、亮度与透明度全解析
  • 竞赛选题 深度学习YOLOv5车辆颜色识别检测 - python opencv
  • linux U盘无法使用,提示“Partition table entries are not in disk order“
  • HDLbits: Fsm ps2
  • 【设计模式】八、桥接模式
  • 从零开始的stable diffusion
  • 【Qt之QString】数值与进制字符串间的转换详解
  • Pytest单元测试框架 —— Pytest+Allure+Jenkins的应用
  • 科普向丨语音芯片烧录工艺的要求
  • : 依赖: qtbase5-dev (= 5.12.8+dfsg-0ubuntu2.1) 但是它将不会被安装 或
  • Unity中Camera类实现坐标系转换的示例
  • vue-按键修饰符
  • [初始java]——java为什么这么火,java如何实现跨平台、什么是JDK/JRE/JVM
  • R语言手动绘制NHANSE数据基线表并聊聊NHANSE数据制作亚组交互效应表的问题(P for interaction)
  • C++引用(起别名)
  • Ubuntu:VS Code IDE安装ESP-IDF【保姆级】(草稿)
  • 子序列(All in All, UVa 10340)rust解法
  • AI时代,当项目经理遇到ChatGPT,插上腾飞的翅膀!
  • Springboot项目中加载Groovy脚本并调用其内部方代码实现
  • 为什么要做数据可视化
  • 0基础学习VR全景平台篇 第108篇:全景图细节处理(下,航拍)
  • linux查看文件内容命令more/less/cat/head/tail/grep
  • VBA窗体跟随活动单元格【简易版】
  • epiiAdmin框架注意事项
  • 数据仓库与ETL
  • Centos7安装Gitlab--gitlab--ee版
  • 主题教育问题清单及整改措施2023年-主题教育对照六个方面个人剖析材料
  • php新手实战:自定义书源下载api