【Web 三件套】个人简单博客系统页面搭建(附源码)

x33g5p2x  于2022-02-20 转载在 其他  
字(13.9k)|赞(0)|评价(0)|浏览(364)

1. 成品展示

以下为个人搭建的一个简单博客系统页面,以后会不断改进,并且与后端结合,形成一个完整的博客系统

2. 知识准备

该博客系统页面是由 HTML + CSS + JavaScript 搭建的,如果没有了解过这些知识的友友,可以通过本人之前写好的几篇相关文章入门

  • 文章一:《超多动图带你入门 HTML》
  • 文章二:《两万字入门 CSS》
  • 文章三:《JavaScript 入门知识》
  • 文章四:《JavaScript 的数组、函数、对象》
  • 文章五:《JavaScript WebAPI 介绍》

3. 博客系统页面搭建

3.1 基本介绍

该博客系统页面现由4个部分组成,分别为:博客列表页、博客详情页、博客登录页、博客编辑页

针对每个页面后面都会进行页面和代码的展示,并且代码中含有个人的注释。为了方便下载使用,下面附上该博客系统页面的 Gitee 链接

Gitee 链接:https://gitee.com/bbbbbge/blog-system

3.2 博客列表页

页面展示:

页面元素 HTML 代码:blog_list.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>博客列表页</title>
  8. <link rel="stylesheet" href="css/common.css">
  9. <link rel="stylesheet" href="css/blog_list.css">
  10. </head>
  11. <body>
  12. <!-- 导航栏 -->
  13. <div class="nav">
  14. <img src="image/log.png" alt="">
  15. <span class="title">我的博客系统</span>
  16. <!-- 使用 span 把左右两侧的元素给撑开 -->
  17. <span class="spacer"></span>
  18. <a href="blog_list.html">主页</a>
  19. <a href="blog_edit.html">写博客</a>
  20. <a href="blog_login.html">注销</a>
  21. </div>
  22. <!-- 版心 -->
  23. <div class="container">
  24. <!-- 左侧区域,显示用户信息 -->
  25. <div class="container-left">
  26. <!-- 用户详情 -->
  27. <div class="card">
  28. <!-- 用户的头像 -->
  29. <img src="image/head.jpg" alt="">
  30. <!-- 用户名 -->
  31. <h3>吞吞吐吐大魔王</h3>
  32. <!-- 其它信息 -->
  33. <a href="https://blog.csdn.net/weixin_51367845?type=blog">CSDN 地址</a>
  34. <a href="#">GitHub 地址</a>
  35. <!-- 文章分类 -->
  36. <div class="counter">
  37. <span>文章</span>
  38. <span>分类</span>
  39. </div>
  40. <div class="counter">
  41. <span>2</span>
  42. <span>1</span>
  43. </div>
  44. </div>
  45. </div>
  46. <!-- 右侧区域,显示博客列表 -->
  47. <div class="container-right">
  48. <!-- 每个 blog 代表一篇博客 -->
  49. <div class="blog">
  50. <div class="title">第一篇博客</div>
  51. <div class="date">2022-2-16</div>
  52. <div class="desc">
  53. 中国人的性情是喜欢调和折中的,譬如你说,这屋子太暗,须在这里开一个窗,大家一定不允许的。但如果你主张拆掉屋顶他们就来调和,愿意开窗了。
  54. </div>
  55. <a href="blog_detail.html" class="detail">查看全文&gt;&gt;</a>
  56. </div>
  57. <div class="blog">
  58. <div class="title">第二篇博客</div>
  59. <div class="date">2022-2-16</div>
  60. <div class="desc">
  61. 中国人的性情是喜欢调和折中的,譬如你说,这屋子太暗,须在这里开一个窗,大家一定不允许的。但如果你主张拆掉屋顶他们就来调和,愿意开窗了。
  62. </div>
  63. <a href="blog_detail.html" class="detail">查看全文&gt;&gt;</a>
  64. </div>
  65. </div>
  66. </div>
  67. </body>
  68. </html>

专属页面样式 CSS 代码:blog_list.css

  1. /* 这个 CSS 专门针对博客列表页来设置样式 */
  2. .blog {
  3. width: 100%;
  4. padding: 10px 20px;
  5. }
  6. /* 博客的标题 */
  7. .blog .title {
  8. text-align: center;
  9. font-size: 25px;
  10. font-weight: 700;
  11. padding-top: 10px;
  12. padding-bottom: 5px;
  13. }
  14. /* 博客的日期 */
  15. .blog .date {
  16. text-align: center;
  17. padding-bottom: 10px;
  18. color: grey;
  19. }
  20. /* 博客的描述 */
  21. .blog .desc {
  22. text-indent: 2em;
  23. }
  24. /* 查看博客详情的按钮 */
  25. .blog .detail {
  26. display: block;
  27. width: 120px;
  28. color: grey;
  29. height: 30px;
  30. /* 设置边框 */
  31. border: 2px solid grey;
  32. /* 文字水平居中 */
  33. text-align: center;
  34. /* 文字垂直居中 */
  35. line-height: 30px;
  36. /* 去掉下划线 */
  37. text-decoration: none;
  38. /* 让按钮来到屏幕中间 */
  39. margin: 10px auto;
  40. /* 加上一个过度效果 */
  41. transition: all 1s;
  42. }
  43. /* 实现鼠标悬停在按钮上时有一个背景色切换的效果 */
  44. .blog .detail:hover {
  45. background-color: grey;
  46. color: white;
  47. }

3.3 博客详情页

页面展示:

页面元素 HTML 代码:blog_detail.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>博客详情页</title>
  8. <link rel="stylesheet" href="css/common.css">
  9. <link rel="stylesheet" href="css/blog_detail.css">
  10. </head>
  11. <body>
  12. <!-- 导航栏 -->
  13. <div class="nav">
  14. <img src="image/log.png" alt="">
  15. <span class="title">我的博客系统</span>
  16. <!-- 使用 span 把左右两侧的元素给撑开 -->
  17. <span class="spacer"></span>
  18. <a href="blog_list.html">主页</a>
  19. <a href="blog_edit.html">写博客</a>
  20. <a href="blog_login.html">注销</a>
  21. </div>
  22. <!-- 版心 -->
  23. <div class="container">
  24. <!-- 左侧区域,显示用户信息 -->
  25. <div class="container-left">
  26. <!-- 用户详情 -->
  27. <div class="card">
  28. <!-- 用户的头像 -->
  29. <img src="image/head.jpg" alt="">
  30. <!-- 用户名 -->
  31. <h3>吞吞吐吐大魔王</h3>
  32. <!-- 其它信息 -->
  33. <a href="https://blog.csdn.net/weixin_51367845?type=blog">CSDN 地址</a>
  34. <a href="#">GitHub 地址</a>
  35. <!-- 文章分类 -->
  36. <div class="counter">
  37. <span>文章</span>
  38. <span>分类</span>
  39. </div>
  40. <div class="counter">
  41. <span>2</span>
  42. <span>1</span>
  43. </div>
  44. </div>
  45. </div>
  46. <!-- 右侧区域,显示博客列表 -->
  47. <div class="container-right">
  48. <!-- 使用这个 div 来放博客内容 -->
  49. <div class="blog-content">
  50. <!-- 博客的标题 -->
  51. <h3>我的第一篇博客</h3>
  52. <!-- 博客的日期 -->
  53. <div class="date">2022-2-16</div>
  54. <!-- 博客的内容 -->
  55. <div class="detail">
  56. <p>
  57. 中国人的性情是喜欢调和折中的,譬如你说,这屋子太暗,须在这里开一个窗,大家一定不允许的。但如果你主张拆掉屋顶他们就来调和,愿意开窗了。
  58. </p>
  59. <p>
  60. 而忽而这些都空虚了,但有时故意地填以没奈何的自欺的希望。希望,希望,用这希望的盾,抗拒那空虚中的暗夜的袭来,虽然盾后面也依然是空虚中的暗夜。
  61. </p>
  62. <p>
  63. 如果痛苦换来的是结识真理、坚持真理,就应自觉的欣然承受,那时,也只有那时,痛苦穿掘着灵魂的深处,使人受了精神底苦刑而得到创伤,又即从这得伤和养伤和愈合中,得到苦的涤除,而上了苏生的路。
  64. </p>
  65. <p>
  66. 当我沉默着的时候,我觉得充实;我将开口,同时感到空虚。过去的生命已经死亡。我对于这死亡有大欢喜,因为我借此知道它曾经存活。死亡的生命已经朽腐。我对于这朽腐有大欢喜,因为我借此知道它还非空虚。
  67. </p>
  68. </div>
  69. </div>
  70. </body>
  71. </html>

专属页面样式 CSS 代码:blog_detail.css

  1. /* 这个 CSS 文件是用来放博客详情页的专属样式 */
  2. .blog-content {
  3. padding: 30px;
  4. }
  5. /* 博客标题 */
  6. .blog-content h3 {
  7. text-align: center;
  8. padding-top: 40px;
  9. padding-bottom: 20px;
  10. font-size: 25px;
  11. }
  12. /* 博客日期 */
  13. .blog-content .date {
  14. text-align: center;
  15. padding-bottom: 10px;
  16. color: grey;
  17. }
  18. /* 博客内容 */
  19. .blog-content p {
  20. text-indent: 2em;
  21. padding: 10px 0;
  22. }

3.4 博客登录页

页面展示:

页面元素 HTML 代码:blog_login.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>登录页</title>
  8. <link rel="stylesheet" href="css/common.css">
  9. <link rel="stylesheet" href="css/blog-login.css">
  10. </head>
  11. <body>
  12. <!-- 导航栏 -->
  13. <div class="nav">
  14. <img src="image/log.png" alt="">
  15. <span class="title">我的博客系统</span>
  16. <!-- 使用 span 把左右两侧的元素给撑开 -->
  17. <span class="spacer"></span>
  18. <a href="blog_list.html">主页</a>
  19. <a href="blog_edit.html">写博客</a>
  20. <a href="blog_login.html">注销</a>
  21. </div>
  22. <!-- 登录页面的版心 -->
  23. <div class="login-container">
  24. <div class="login-dialog">
  25. <h3>登录</h3>
  26. <div class="row1">
  27. <span>用户名</span>
  28. <input type="text" id="username">
  29. </div>
  30. <div class="row1">
  31. <span>密码</span>
  32. <input type="password" id="password">
  33. </div>
  34. <div class="row2">
  35. <input type="button" value="提交" id="submit">
  36. </div>
  37. </div>
  38. </div>
  39. </body>
  40. </html>

专属页面样式 CSS 代码:blog_login.css

  1. /* 这个 CSS 文件是放登陆页面的专属样式 */
  2. /* 1. 设置版心,让这个版心将页面填充满 */
  3. /* 2. 在版心中间位置,设置一个登陆区域 */
  4. .login-container {
  5. width: 100%;
  6. height: calc(100% - 50px);
  7. display: flex;
  8. justify-content: center;
  9. align-items: center;
  10. }
  11. .login-container .login-dialog {
  12. width: 400px;
  13. height: 280px;
  14. background-color: rgba(255, 255, 255, 0.3);
  15. border-radius: 20px;
  16. }
  17. .login-dialog h3 {
  18. text-align: center;
  19. padding: 30px;
  20. font-size: 25px;
  21. }
  22. .login-dialog .row1 {
  23. height: 40px;
  24. display: flex;
  25. align-items: center;
  26. justify-content: center;
  27. }
  28. .login-dialog .row2 {
  29. height: 40px;
  30. display: flex;
  31. align-items: center;
  32. justify-content: center;
  33. padding-top: 30px;
  34. }
  35. .login-dialog .row1 span {
  36. width: 80px;
  37. font-weight: 700;
  38. }
  39. .login-dialog .row1 input {
  40. width: 200px;
  41. height: 30px;
  42. font-size: 15px;
  43. padding-left: 10px;
  44. background-color: rgba(255, 255, 255, 0.3);
  45. /* 去掉边框 */
  46. border: none;
  47. /* 去掉轮廓线(选中输入框时,外面的黑边) */
  48. outline: none;
  49. }
  50. .login-dialog .row2 input {
  51. width: 280px;
  52. height: 35px;
  53. border-radius: 8px;
  54. font-size: 20px;
  55. background-color: rgba(41, 83, 149, 0.7);
  56. border: none;
  57. }
  58. .login-dialog .row2 input:active {
  59. background-color: black;
  60. color: white;
  61. }

3.5 博客编辑页

页面展示:

页面元素 HTML 代码:blog_edit.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>博客编辑页</title>
  8. <link rel="stylesheet" href="css/common.css">
  9. <link rel="stylesheet" href="css/blog_edit.css">
  10. <!-- 引入依赖 -->
  11. <link rel="stylesheet" href="editor.md-master/css/editormd.min.css">
  12. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  13. <script src="editor.md-master/lib/marked.min.js"></script>
  14. <script src="editor.md-master/lib/prettify.min.js"></script>
  15. <script src="editor.md-master/editormd.js"></script>
  16. </head>
  17. <body>
  18. <!-- 导航栏 -->
  19. <div class="nav">
  20. <img src="image/log.png" alt="">
  21. <span class="title">我的博客系统</span>
  22. <!-- 使用 span 把左右两侧的元素给撑开 -->
  23. <span class="spacer"></span>
  24. <a href="blog_list.html">主页</a>
  25. <a href="blog_edit.html">写博客</a>
  26. <a href="blog_login.html">注销</a>
  27. </div>
  28. <!-- 版心 -->
  29. <div class="blog-edit-container">
  30. <!-- 标题编辑区 -->
  31. <div class="title">
  32. <input type="text" placeholder="在这里写下文章标题" id="title">
  33. <input type="button" value="发布文章" id="submit">
  34. </div>
  35. <!-- 内容编辑区 -->
  36. <div id="editor">
  37. </div>
  38. </div>
  39. <script>
  40. // 初始化 editor.md
  41. let editor = editormd("editor", {
  42. // 这里的尺寸必须在这里设置,设置样式会被 editormd 自动覆盖
  43. width: "100%",
  44. // 设定编辑高度
  45. height: "500px",
  46. // 编辑页中的初始化内容
  47. markdown: "# 在这里写下一篇博客",
  48. //指定 editor.md 依赖的插件路径
  49. path: "editor.md-master/lib/"
  50. });
  51. </script>
  52. </body>
  53. </html>

专属页面样式 CSS 代码:blog_edit.css

  1. /* 这个 CSS 文件放博客编辑的专属样式 */
  2. /* 博客编辑页的版心 */
  3. .blog-edit-container {
  4. width: 1000px;
  5. height: calc(100% - 50px);
  6. margin: 0 auto;
  7. }
  8. /* 标题编辑区 */
  9. .blog-edit-container .title {
  10. margin-top: 20px;
  11. width: 100%;
  12. height: 60px;
  13. display: flex;
  14. align-items: center;
  15. justify-content: space-between;
  16. }
  17. .blog-edit-container .title #title {
  18. width: 795px;
  19. height: 50px;
  20. border-radius: 10px;
  21. padding-left: 20px;
  22. border: none;
  23. outline: none;
  24. background-color: rgba(41, 83, 149, 0.3);
  25. font-size: 15px;
  26. }
  27. .blog-edit-container .title #submit {
  28. width: 200px;
  29. height: 50px;
  30. border-radius: 10px;
  31. border: none;
  32. outline: none;
  33. background-color: rgba(255, 192 , 153, 0.4);
  34. font-size: 20px;
  35. }
  36. .blog-edit-container .title #submit:active {
  37. background-color: rgba(41, 83, 149, 0.3);
  38. }
  39. #editor {
  40. border-radius: 10px;
  41. border: none;
  42. opacity: 0.7;
  43. }

3.6 公共页面样式

公共页面样式 CSS 代码:common.css

  1. /* 这个文件里面放4个页面公共的一些样式,比如背景、导航栏等 */
  2. * {
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. }
  7. html, body {
  8. /* 和父元素的一样高,body 的父亲是 html,html 的父亲是浏览器窗口 */
  9. height: 100%;
  10. /* 页面背景 */
  11. background-image: url(../image/background.jpg);
  12. background-position: center center;
  13. background-repeat: no-repeat;
  14. background-size: cover;
  15. }
  16. /* 导航栏 */
  17. .nav {
  18. width: 100%;
  19. height: 50px;
  20. background-color: rgba(50, 50, 50, 0.2);
  21. color: #fff;
  22. display: flex;
  23. justify-content: start;
  24. align-items: center;
  25. }
  26. /* 导航栏中的 log */
  27. .nav img {
  28. width: 40px;
  29. height: 40px;
  30. margin-left: 30px;
  31. margin-right: 10px;
  32. }
  33. /* 导航栏中的标题 */
  34. .nav .title {
  35. width: 120px;
  36. }
  37. /* 导航栏中的占位符 */
  38. .nav .spacer {
  39. height: 40px;
  40. width: calc(100% - 370px);
  41. }
  42. /* 导航栏中的链接 */
  43. .nav a {
  44. width: 60px;
  45. margin: 20px;
  46. text-decoration: none;
  47. color: white;
  48. }
  49. /* 版心的样式 */
  50. .container {
  51. height: calc(100% - 50px);
  52. width: 1000px;
  53. margin: 0 auto;
  54. /* 为了让版心里面的子元素能够以左右布局的方式显示,使用 flex 布局 */
  55. display: flex;
  56. align-items: center;
  57. justify-content: space-between;
  58. }
  59. /* 左侧区域样式 */
  60. .container .container-left {
  61. height: 100%;
  62. width: 200px;
  63. }
  64. /* 右侧区域样式 */
  65. .container .container-right {
  66. height: 100%;
  67. width: 795px;
  68. background-color: rgba(255, 255, 255, 0.3);
  69. border-radius: 20px;
  70. }
  71. /* 用户信息卡片,也会在多个页面中用到 */
  72. .card {
  73. background-color: rgba(255, 255, 255, 0.3);
  74. border-radius: 20px;
  75. padding: 30px;
  76. }
  77. /* 用户头像 */
  78. .card img {
  79. width: 140px;
  80. height: 140px;
  81. border-radius: 50%;
  82. }
  83. /* 用户名 */
  84. .card h3 {
  85. text-align: center;
  86. padding: 10px 0;
  87. }
  88. /* 其它信息 */
  89. .card a {
  90. /* a 标签是行内元素,设置尺寸、边距都可能失效,要转成块级元素 */
  91. display: block;
  92. text-decoration: none;
  93. text-align: center;
  94. color: grey;
  95. padding: 3px;
  96. }
  97. .card .counter {
  98. display: flex;
  99. justify-content: space-around;
  100. font-size: 15px;
  101. padding-top: 5px;
  102. }

3.7 markdown 编辑器引入

在博客编辑页中含有一个 markdown 编辑器,这个是直接引入了一个叫 editor.md 的开源的页面 markdown 编辑器组件,大家也可以使用其它的编辑器组件

引入 editor.md 步骤:

  1. 下载 editor.md
    大家直接进入该项目的官网进行下载即可

官网地址为:https://pandao.github.io/editor.md/

  1. 将下载好的 editor.md 接压缩到该博客项目中

  1. 在博客编辑页中引入 editor.md 依赖
  1. <!-- 引入依赖 -->
  2. <link rel="stylesheet" href="editor.md-master/css/editormd.min.css">
  3. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  4. <script src="editor.md-master/lib/marked.min.js"></script>
  5. <script src="editor.md-master/lib/prettify.min.js"></script>
  6. <script src="editor.md-master/editormd.js"></script>
  1. 在博客编辑页中,写个 JS 对 editor.md 进行初始化
  1. // 初始化 editor.md
  2. let editor = editormd("editor", {
  3. // 这里的尺寸必须在这里设置,设置样式会被 editormd 自动覆盖
  4. width: "100%",
  5. // 设定编辑高度
  6. height: "500px",
  7. // 编辑页中的初始化内容
  8. markdown: "# 在这里写下一篇博客",
  9. //指定 editor.md 依赖的插件路径
  10. path: "editor.md-master/lib/"
  11. });

通过以上步骤,就可以将 editor.md 该 markdown 编辑器引入我们的博客编辑页了

4. 总结

该博客系统页面只是运用前段时间学习的前端知识做出来的一个简单的页面,往后会通过不断的学习来对该博客系统进行补充,期待它的成品!

相关文章