赞
踩
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> *{ margin: 0; padding: 0; } .wrap{ padding: 20px; background-color: pink; } .box{ max-height: 40px; overflow-y: hidden; line-height: 20px; font-size: 14px; } </style> </head> <body> <div class="wrap"> <div class="box"> <p class="content">Vue.js 使用了基于 HTML 的模板语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据。所有 Vue.js 的模板都是合法的 HTML,所以能被遵循规范的浏览器和 HTML 解析器解析。</p> </div> </div> <script> const box = document.querySelector('.box') const content = document.querySelector('.content') console.log(box.clientHeight, content.offsetHeight) if(content.offsetHeight > box.clientHeight) { // 拷贝追加一份子节点,撑开内容高度,使一份内容可以完全滚完 const content2 = document.createElement('p') content2.innerHTML = content.innerHTML box.appendChild(content2) setInterval(() => { // 滚完一份内容的高度后,重置滚动位移 if(box.scrollTop >= content.offsetHeight){ box.scrollTop -= content.offsetHeight }else { // 自行滚动 box.scrollTop++ } }, 200) } </script> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。