动态显示代码

  1. let message = `此处是css代码`
  2. let n = 0
  3. function showPikachu(){
  4. setTimeout(()=>{
  5. n += 1
  6. text.innerText = message.substr(0,n)
  7. style.innerText = message.substr(0,n)
  8. text.scrollTop = text.scrollHeight // 滚动到底部
  9. if (n < message.length) {
  10. showPikachu()
  11. }
  12. },0)
  13. }
  14. showPikachu()

滚动到底部

scrollTop: 一个元素的内容垂直滚动的像素数
scrollHeight: 一个元素内容高度的度量

  1. window.scrollTo(0, 9999999)
  2. xxx.scrollTop = xxx.scrollHeight

隐藏滚动条

超出页面处理

  1. overflow-y: auto; /* auto浏览器处理,hidden不滚动,scroll总是显示滚动条 */

隐藏滚动条,但能用滚轮

  1. #text::-webkit-scrollbar {
  2. display:none
  3. }

JS导入模块

在一个js文件中写入

  1. let str = "xxx"
  2. export default str

在另一个文件中导入

  1. import x from "./outer.js" // x可以自定义
  2. // 后面的代码直接使用x
  3. console.log(x)

代码重构

  1. import string from './style.js'
  2. const player = {
  3. n: 0,
  4. time: 60,
  5. id: undefined,
  6. code: '',
  7. init: ()=>{
  8. player.listener()
  9. player.play()
  10. },
  11. ui:{
  12. text: document.querySelector('#text'),
  13. style: document.querySelector('#style')
  14. },
  15. events: {
  16. '#btnPause' : 'pause',
  17. '#btnPlay' : 'play',
  18. '#btnSlow' : 'slow',
  19. '#btnNormal' : 'normal',
  20. '#btnQuick' : 'quick',
  21. '#btnReplay' : 'replay'
  22. },
  23. listener: ()=>{
  24. for(const key in player.events)
  25. if(player.events.hasOwnProperty(key)){
  26. document.querySelector(key).onclick = player[player.events[key]]
  27. }
  28. },
  29. play: ()=>{
  30. player.id = setInterval(player.run, player.time)
  31. },
  32. pause: ()=> {
  33. window.clearInterval(player.id)
  34. },
  35. run: ()=> {
  36. if(player.n < string.length) {
  37. if (string[player.n] === "\n") {
  38. player.code += "<br>";
  39. } else if (string[player.n] === " ") {
  40. player.code += "&nbsp;"; // HTML的空格
  41. } else {
  42. player.code += string[player.n];
  43. }
  44. player.n += 1
  45. player.ui.text.innerHTML = player.code
  46. player.ui.style.innerHTML = string.substring(0,player.n)
  47. player.ui.text.scrollTop = text.scrollHeight
  48. }
  49. },
  50. slow: () => {
  51. player.pause()
  52. player.time = 200
  53. player.play()
  54. },
  55. normal: () => {
  56. player.pause()
  57. player.time = 60
  58. player.play()
  59. },
  60. quick: () => {
  61. player.pause()
  62. player.time = 0
  63. player.play()
  64. },
  65. replay: () => {
  66. player.pause()
  67. player.n = 0
  68. player.play()
  69. }
  70. }
  71. player.init()

媒体查询

媒体查询一定要写在默认样式的后面,不然会被覆盖,不起作用

  1. .pikachu {
  2. margin-top: 10px;
  3. font-size: 100px;
  4. }
  5. @media(max-width: 500px){
  6. .pikachu{
  7. font-size: 70px;
  8. }
  9. }