Django web开发(一) - 前端

慈云数据 2024-03-27 技术支持 51 0

文章目录

  • 前端开发
    • 1.快速开发网站
    • 2.标签
      • 2.1 编码
      • 2.2 title
      • 2.3 标题
      • 2.4 div和span
      • 2.5 超链接
      • 2.6 图片
      • 小结
      • 标签的嵌套
      • 2.7 列表
      • 2.8 表格
      • 2.9 input系列
      • 2.10 下拉框
      • 2.11 多行文本
      • 用户注册
      • 案例: 用户注册
        • GET 方式
        • POST 方式
        • 表单数据提交优化
        • 3.CSS样式
          • 3.1 快速上手
          • 3.2 CSS应用方式
            • 1. 在标签上
            • 2. 在 head 标签的 style 上
            • 3. 写到文件中
            • 3.3 选择器
              • 1. ID选择器
              • 2. 类选择器
              • 3. 标签选择器
              • 4. 属性选择器
              • 5. 后代选择器
              • 关于样式的覆盖问题
              • 3.4 样式
                • 1. 高度和宽度
                • 2. 块级和行内标签
                • 3. 字体和对齐方式
                • 4. 浮动
                • 5. 内边距
                • 6. 外边距
                • 7. hover
                • 8. after
                • 9. position
                  • 9.1 fixed
                  • 9.2 relative和absolute
                  • 10. border
                  • 11. 背景色
                  • 4.案例: 小米商城
                    • 4.1 小米顶部
                    • 4.2 二级菜单
                    • 4.3 推荐区域
                    • 5. Bootstrap
                      • 5.1 初识Bootstrap
                      • 5.2 栅格系统
                      • 5.3 container
                      • 5.4 面板
                      • 5.5 媒体对象
                      • 5.6 分页
                      • 案例: 登录
                      • 案例: 后台管理
                      • 5.7 图标
                      • 优化
                      • 5.8 Bootstrap实现动态效果
                      • 6. Javascript
                        • 6.1 代码位置
                        • 6.2 注释
                        • 6.3 变量
                          • 6.3.1 字符串类型
                          • 案例: 跑马灯
                          • 6.3.2 数组
                          • 案例: 动态数据
                          • 6.3.3 对象(字典)
                          • 案例: 动态表格
                          • 6.4 条件语句
                          • 6.5 函数
                          • 6.6 DOM
                            • 事件的绑定
                            • 7. JQuery
                              • 7.1 快速上手
                              • 7.2 寻找标签(直接)
                                • 7.2.1 ID选择器
                                • 7.2.2 样式选择器
                                • 7.2.3 标签选择器
                                • 7.2.4 层级选择器
                                • 7.2.5 多选择器
                                • 7.2.6 属性选择器
                                • 7.3 寻找标签(间接)
                                  • 7.3.1 找到上一个兄弟
                                  • 7.3.2 找父子
                                  • 案例: 菜单的切换
                                  • 7.4 值的操作
                                  • 案例: 动态创建数据
                                  • 7.5 事件
                                  • 案例: 表格操作
                                  • 8. 前端整合
                                    • 案例: 添加数据页面

                                      前端开发

                                      说明:

                                      • 本人的实验环境在云服务器(CentOS7操作系统)上,在VSCode软件上SSH远程连接云服务器进行代码编写
                                      • 因为文章所有内容和代码是纯手敲的缘故,所以演示的代码可能跟武沛齐(据说是小猪佩奇的远房表哥)老师视频中的有所不同,但原理相同
                                        目的: 开发一个平台
                                        	- 前端开发: HTML CSS JavaScript
                                        	- 接收请求并处理
                                        	- Mysql数据库: 存储数据
                                        快速上手: 
                                        	基于Flask Web框架快速搭建一个网站
                                        深入学习:
                                        	基于Django框架
                                        

                                        1.快速开发网站

                                        python 安装 Flask web 框架

                                        pip install flask
                                        

                                        创建Flask的python目录

                                        [root@hecs-33592 ~]# mkdir -p /root/python/FlaskWeb
                                        [root@hecs-33592 ~]# cd /root/python/FlaskWeb
                                        [root@hecs-33592 FlaskWeb]# pwd
                                        /root/python/FlaskWeb
                                        

                                        创建一个名为web.py的python文件

                                        from flask import Flask
                                        app = Flask(__name__)
                                        # 创建了网址 /show/info 和 函数index 的对应关系
                                        # 以后用户在浏览器上访问 /show/info, 网站自动执行
                                        @app.route("/show/info")
                                        def index():
                                            return "中国联通"
                                        if __name__ == '__main__':
                                            app.run(host='0.0.0.0', port=5100, debug=False)  
                                        

                                        运行

                                        [root@hecs-33592 ~]# /usr/bin/python3 /root/python/FlaskWeb/web.py
                                        

                                        run_flask_web

                                        浏览器进行访问: http://[你的ip]:5100/show/info

                                        web1

                                        这种 return 方式返回 HTML 内容的方式不方便进行管理,因此我们会引入templates模板

                                        from flask import Flask, render_template
                                        app = Flask(__name__)
                                        # 创建了网址 /show/info 和 函数index 的对应关系
                                        # 以后用户在浏览器上访问 /show/info, 网站自动执行
                                        @app.route("/show/info")
                                        def index():
                                            # 默认去当前目录的 templates 文件夹中找
                                            return render_template("index.html")
                                        if __name__ == '__main__':
                                            app.run(host='0.0.0.0', port=5100, debug=False)
                                        

                                        创建templates目录

                                        mkdir /root/python/FlaskWeb/templates/
                                        

                                        编写index.html文件

                                        在这里插入图片描述

                                        
                                        
                                            
                                            
                                            
                                            Document
                                        
                                        
                                            

                                        中国联通

                                        重新运行Flask,浏览器刷新访问

                                        在这里插入图片描述

                                        当然这个templates目录也可以自定义名称

                                        # 例如目录名称为"xxx"
                                        app = Flask(__name__, template_folder="xxx")
                                        

                                        2.标签

                                        2.1 编码

                                         
                                        

                                        2.2 title

                                            
                                            
                                            
                                            Document
                                        
                                        

                                        2.3 标题

                                            

                                        一级标题

                                        二级标题

                                        三级标题

                                        四级标题

                                        五级标题

                                        2.4 div和span

                                        内容
                                        asd
                                        • div: 占一整行(块级标签)
                                        • span: 用多少占多少(行内标签/内联标签)
                                          • 两个 span 标签不在同一行,页面显示时会在同一行,中间以一个空格分隔
                                          • 两个 span 标签在同一行,页面显示时会在同一行,中间没有空格,连着

                                            2.5 超链接

                                            这里就很有意思了,你可以选择跳转自己网站的地址,或者跳转外部的网站

                                            
                                            
                                                
                                                
                                                
                                                我的联通
                                            
                                            
                                                点击跳转自己的网站
                                                点击跳转别人的网站百度
                                            
                                            
                                            

                                            然后需要修改web.py文件

                                            from flask import Flask, render_template
                                            app = Flask(__name__)
                                            # 创建了网址 /show/info 和 函数index 的对应关系
                                            # 以后用户在浏览器上访问 /show/info, 网站自动执行
                                            @app.route("/show/info")
                                            def index():
                                                # 默认去当前目录的 templates 文件夹中找
                                                return render_template("index.html")
                                            # 新添加如下配置
                                            @app.route("https://blog.csdn.net/get/news")
                                            def get_news():
                                                # 默认去当前目录的 templates 文件夹中找
                                                return render_template("get_news.html")
                                            if __name__ == '__main__':
                                                app.run(host='0.0.0.0', port=5100, debug=False)
                                            

                                            在templates目录下新添加一个 get_news.html 文件

                                            
                                            
                                                
                                                
                                                
                                                Document
                                            
                                            
                                                

                                            我是内部链接

                                            重新运行Flask,刷新页面

                                            在这里插入图片描述

                                            点击第一行后,跳转到如下页面

                                            在这里插入图片描述点击点击第二行后,跳转到百度

                                            自行脑补百度页面哈

                                            在新的 Tab 标签页打开链接

                                            添加 target=“_blank”

                                                
                                                    
                                            
                                            

                                            2.6 图片

                                                

                                            我是内部链接

                                            在这里插入图片描述

                                            刷新浏览器

                                            在这里插入图片描述

                                            尝试访问服务器本地图片

                                            在/root/python/FlaskWeb/下新建目录static

                                            放入一张图片dog.jpg

                                            在这里插入图片描述

                                            修改get_news.html

                                                

                                            我是内部链接

                                            刷新浏览器

                                            在这里插入图片描述

                                            跟刚才一样

                                            然后可以调整一下图片的高度与宽度

                                                

                                            我是内部链接

                                            在这里插入图片描述

                                            小结

                                            - 块级标签
                                            	- 

                                            -
                                            - 行内标签 - - -

                                            标签的嵌套

                                            实现: 点击图片,跳转至指定页面

                                            修改web.py,增加get_product

                                            from flask import Flask, render_template
                                            app = Flask(__name__)
                                            # 创建了网址 /show/info 和 函数index 的对应关系
                                            # 以后用户在浏览器上访问 /show/info, 网站自动执行
                                            @app.route("/show/info")
                                            def index():
                                                # 默认去当前目录的 templates 文件夹中找
                                                return render_template("index.html")
                                            @app.route("https://blog.csdn.net/get/news")
                                            def get_news():
                                                return render_template("get_news.html")
                                            @app.route("/get/product")
                                            def get_product():
                                                return render_template("get_product.html")
                                            if __name__ == '__main__':
                                                app.run(host='0.0.0.0', port=5100, debug=False)
                                            

                                            在templates下新增一个get_product.html

                                            
                                            
                                                
                                                
                                                
                                                Document
                                            
                                            
                                                
                                                    
                                            
                                            
                                            

                                            访问页面

                                            在这里插入图片描述

                                            点击图片进行url跳转

                                            在这里插入图片描述

                                            2.7 列表

                                            无序列表

                                            在这里插入图片描述

                                            在这里插入图片描述

                                            有序列表

                                            1. 中国移动
                                            2. 中国联通
                                            3. 中国电信

                                            在这里插入图片描述

                                            2.8 表格

                                            修改web.py新增一个访问路径

                                            @app.route("/get/table")
                                            def get_table():
                                                return render_template("get_table.html")
                                            

                                            在templates页面下新建get_table.html文件

                                            
                                            
                                                
                                                
                                                
                                                Document
                                            
                                            
                                                
                                            ID姓名年龄
                                            10张三20
                                            11李四20
                                            12王五20
                                            13赵六20

                                            重新运行并访问页面

                                            在这里插入图片描述

                                            为表格增加边框

                                            在这里插入图片描述

                                            2.9 input系列

                                            
                                            
                                            
                                            
                                            
                                            男
                                            女
                                            
                                            唱
                                            跳
                                            Rap
                                            篮球
                                            
                                            	普通按钮
                                            	提交表单
                                            

                                            2.10 下拉框

                                            	北京
                                            	上海
                                            	深圳
                                            
                                            

                                            2.11 多行文本

                                            用户注册

                                            修改web.py

                                            @app.route("/register")
                                            def register():
                                                return render_template("register.html")
                                            

                                            在templates下新建register.html

                                            
                                            
                                                
                                                
                                                
                                                Document
                                            
                                            
                                                

                                            用户注册

                                            用户名:
                                            密码:
                                            性别: 男 女
                                            爱好: 唱 跳 Rap 篮球
                                            城市: 北京 上海 深圳
                                            备注:

                                            在这里插入图片描述

                                            顺便说一下 GET 方法与 POST 方法的区别

                                            GET: 可通过URL/表单提交

                                            POST: 只能通过表单提交,提交数据不在URL而是在请求体中

                                            案例: 用户注册

                                            新建项目

                                            在/root/python下新建目录:

                                            • account
                                            • template

                                              在account下新建app.py文件

                                              from flask import Flask, render_template
                                              app = Flask(__name__)
                                              @app.route('/register')
                                              def register():
                                                  return render_template('register.html')
                                              if __name__ == '__main__':
                                                  app.run(host='0.0.0.0', port=5200, debug=False)
                                              

                                              在templates下新建register.html文件

                                              
                                              
                                                  
                                                  
                                                  
                                                  Document
                                              
                                              
                                                  

                                              用户注册

                                              运行,浏览器进行访问

                                              在这里插入图片描述

                                              表单可以提交的前提条件:

                                              • 提交方式: method=“get”
                                              • 提交地址: action=“/xxx/xxx/xxx”
                                              • 在form标签里面必须有一个submit标签
                                              • 每个标签有name属性

                                                接下来尝试接收用户提交的表单数据

                                                GET 方式

                                                修改app.py,导入request方法,使用/do/register接收用户数据并展示

                                                from flask import Flask, render_template, request
                                                app = Flask(__name__)
                                                @app.route('/register', methods=['GET'])
                                                def register():
                                                    return render_template('register.html')
                                                @app.route("/do/register", methods=['GET'])
                                                def do_register():
                                                    get_info = request.args
                                                    return get_info
                                                if __name__ == '__main__':
                                                    app.run(host='0.0.0.0', port=5200, debug=True)
                                                

                                                修改templates下的register.html

                                                点击注册后跳转至路由/do/register

                                                
                                                
                                                    
                                                    
                                                    
                                                    Document
                                                
                                                
                                                    

                                                用户注册

                                                用户名:
                                                密码:

                                                在这里插入图片描述

                                                在这里插入图片描述

                                                POST 方式

                                                修改app.py

                                                @app.route("/post/register", methods=['POST'])
                                                def post_register():
                                                    get_info = request.form
                                                    return get_info
                                                

                                                修改register.html

                                                    

                                                用户注册

                                                用户名:
                                                密码:

                                                浏览器访问

                                                在这里插入图片描述

                                                在这里插入图片描述

                                                可以发现,跟上面的GET方法不同的是, 提交后跳转的页面的URL后并没有我们提交的参数,而是提交到了后台

                                                表单数据提交优化

                                                修改register.html

                                                添加 name 与 value 属性

                                                 
                                                

                                                在这里插入图片描述

                                                在这里插入图片描述

                                                在控制台输出数据

                                                修改app.py

                                                @app.route("/post/register", methods=['POST'])
                                                def post_register():
                                                    get_info = request.form
                                                    username = request.form.get("username")
                                                    passwd = request.form.get("passwd")
                                                    sex = request.form.get("sex")
                                                    hobby_list = request.form.getlist("hobby")
                                                    city = request.form.get("city")
                                                    more = request.form.getlist("textarea")
                                                    print(username, passwd, sex, hobby_list, city, more)
                                                    return get_info
                                                

                                                在这里插入图片描述

                                                在这里插入图片描述

                                                整合GET与POST方法

                                                在这里插入图片描述

                                                将上面图片中的内容整合

                                                在这里插入图片描述

                                                @app.route('/register', methods=['GET', 'POST'])
                                                def register():
                                                    if request.method == "GET":
                                                        return render_template('register.html')
                                                    else:
                                                        username = request.form.get("username")
                                                        passwd = request.form.get("passwd")
                                                        sex = request.form.get("sex")
                                                        hobby_list = request.form.getlist("hobby")
                                                        city = request.form.get("city")
                                                        more = request.form.getlist("textarea")
                                                        print(username, passwd, sex, hobby_list, city, more)
                                                        get_info = request.args
                                                        return get_info
                                                

                                                3.CSS样式

                                                3.1 快速上手

                                                3.2 CSS应用方式

                                                1. 在标签上
                                                2. 在 head 标签的 style 上
                                                https://blog.csdn.net/qq_43139145/article/details/...
                                                
                                                    
                                                    Document
                                                    
                                                        .c1 {
                                                            color: red;
                                                        }
                                                    
                                                
                                                
                                                    

                                                用户注册

                                                https://blog.csdn.net/qq_43139145/article/details/...
                                                3. 写到文件中
                                                • common.css
                                                  .c1 {
                                                  	color: red;
                                                  }
                                                  

                                                  调用common.css

                                                  https://blog.csdn.net/qq_43139145/article/details/...
                                                  
                                                      
                                                      Document
                                                      
                                                  
                                                  
                                                      

                                                  用户注册

                                                  https://blog.csdn.net/qq_43139145/article/details/...

                                                  3.3 选择器

                                                  1. ID选择器

                                                  #c1 {
                                                  	color: red;
                                                  }
                                                  

                                                  2. 类选择器

                                                  .c1 {
                                                  	color: red;
                                                  }
                                                  

                                                  3. 标签选择器

                                                  div{
                                                  	color: red;
                                                  }
                                                  
                                                  xxx

                                                  4. 属性选择器

                                                  下面的例子中,所有的text类型的input都会生效

                                                      Document
                                                      
                                                      
                                                          input[type="text"]{
                                                          border: 1px solid red;
                                                      }
                                                      
                                                  
                                                  

                                                  在这里插入图片描述

                                                  还有另一种方式,看下面的例子

                                                      .v1[xx="456"]{
                                                          color: gold;	
                                                      }
                                                  
                                                  https://blog.csdn.net/qq_43139145/article/details/...
                                                  
                                                  https://blog.csdn.net/qq_43139145/article/details/...
                                                      
                                                  a
                                                  b
                                                  c
                                                  https://blog.csdn.net/qq_43139145/article/details/...

                                                  在这里插入图片描述

                                                  5. 后代选择器

                                                  这个选择器很有意思,你可以指定标签让它下面对应的标签全部生效,也可以指定标签让他下面的n级标签生效,具体看例子

                                                      .zz h2{
                                                          color:chartreuse;
                                                      }
                                                  
                                                  
                                                  
                                                      

                                                  我是div里面的h2

                                                  我是div外面的h2

                                                  https://blog.csdn.net/qq_43139145/article/details/...

                                                  在这里插入图片描述

                                                  如果只想让第一层的h1生效,可以添加>号

                                                      .zz > h2{
                                                          color:chartreuse;
                                                      }
                                                  
                                                  

                                                  在这里插入图片描述

                                                  关于样式的覆盖问题

                                                  当一个标签引用了多个 css 样式时,可能会遇到样式属性重复的问题

                                                      .c2 {
                                                          color: darkgoldenrod;
                                                      }
                                                      .c3 {
                                                          color:hotpink;
                                                      }
                                                  
                                                  
                                                      
                                                  我是天才

                                                  在这里插入图片描述

                                                  观察到,c3生效,而c2没有生效,这是因为c3在c2的下面,会将上面的c2属性覆盖掉

                                                  如果不想让上面的被覆盖掉怎么办呢?

                                                  可以在对应的属性后面添加!important

                                                      .c2 {
                                                          color: darkgoldenrod !important;
                                                      }
                                                      .c3 {
                                                          color:hotpink;
                                                      }
                                                  
                                                  

                                                  在这里插入图片描述

                                                  3.4 样式

                                                  1. 高度和宽度

                                                  .c4 {
                                                          height: 300px;
                                                          width: 500px;
                                                      }
                                                  

                                                  注意事项:

                                                  • 支持百分比
                                                  • 行内标签: 默认无效
                                                  • 块级标签: 默认有效(右边的剩余空白区域也会被占用)

                                                    2. 块级和行内标签

                                                    display:inline-block 使行内标签对 height 和 width 生效

                                                    .c4 {
                                                            display: inline-block;
                                                            height: 300px;
                                                            width: 500px;
                                                            border: 1px solid red;
                                                        }
                                                    
                                                    https://blog.csdn.net/qq_43139145/article/details/...
                                                    
                                                    	联通
                                                    
                                                    

                                                    在这里插入图片描述

                                                    块级与行内标签的转换

                                                    	
                                                    移动
                                                    联通

                                                    注意:

                                                    • 块级标签 + 块级&行内标签

                                                      3. 字体和对齐方式

                                                      设置字体颜色/大小/粗细/字体样式

                                                          
                                                          Document
                                                          
                                                              .c1 {
                                                                  color: #00FF7F;                   /* 字体颜色 */
                                                                  font-size: 20px;                  /* 字体大小 */
                                                                  font-weight: 600;                 /* 字体粗细 */
                                                                  font-family: Microsoft Yahei;     /* 字体样式 */
                                                                  text-align: center;               /* 水平方向居中 */
                                                                  line-height: 50px;                /* 垂直方向居中 */
                                                                  border: 1px solid red;            /* 边框 */
                                                              }
                                                          
                                                      
                                                      

                                                      4. 浮动

                                                      如果在块级标签中,加入了float属性,那么这个块级标签奖不会再占用一整行,而是自己有多大就占用多大

                                                      
                                                      
                                                          
                                                          Document
                                                          
                                                              .item {
                                                                  float: left;
                                                                  width: 280px;
                                                                  height: 170px;
                                                                  border: 1px solid red;
                                                              }
                                                          
                                                      
                                                      
                                                          

                                                      在这里插入图片描述

                                                      如果你让标签浮动起来之后,就会脱离文档流。

                                                      例如下面的例子中,我们给div的父标签赋予了一个蓝色的背景,但是你不会看到蓝色背景。因为他被浮动的div字标签挡住了。

                                                          

                                                      解决办法: 在同级子标签的最下面添加

                                                          

                                                      在这里插入图片描述

                                                      5. 内边距

                                                      padding-top | padding-left | padding-right | padding-botton

                                                      
                                                      
                                                          
                                                          Document
                                                          
                                                              .outer {
                                                                  border: 1px solid red;
                                                                  height: 200px;
                                                                  width: 200px;
                                                                  padding-top: 20px;
                                                                  padding-left: 20px;
                                                                  padding-right: 20px;
                                                                  padding-bottom: 20px;
                                                              }
                                                          
                                                      
                                                      
                                                          
                                                      hello
                                                      world

                                                      在这里插入图片描述

                                                      其实上面的四个上下左右的padding可以简写为padding: 20px 20px 20px 20px,顺序为上右下左(顺时针方向)

                                                      6. 外边距

                                                      margin

                                                          

                                                      在这里插入图片描述

                                                      7. hover

                                                      
                                                      
                                                          
                                                          Document
                                                          
                                                              .c1 {
                                                                  color:brown;
                                                              }
                                                              .c1:hover {
                                                                  color: green;
                                                                  font-size: 20px;
                                                              }
                                                              .c2 {
                                                                  width: 300px;
                                                                  height: 300px;
                                                                  border: 3px solid red;
                                                              }
                                                              .c2:hover {
                                                                  border: 3px solid green;
                                                              }
                                                              .download {
                                                                  display: none;
                                                              }
                                                              .app:hover .download {
                                                                  display: block;
                                                              }
                                                          
                                                      
                                                      
                                                          
                                                      字体碰到鼠标变成绿色
                                                      边框碰到鼠标变成绿色
                                                      鼠标放我这里触发显示二维码

                                                      8. after

                                                      
                                                      
                                                          
                                                          Document
                                                          
                                                              .c1:after {
                                                                  content: "大帅比"
                                                              }
                                                          
                                                      
                                                      
                                                          
                                                      张三

                                                      在这里插入图片描述

                                                      after一般像下面这样用,不用每次都写stype="clear: both;"

                                                      
                                                      
                                                          
                                                          Document
                                                          
                                                              .clearfix:after {
                                                                  content: "";
                                                                  display: block;
                                                                  clear: both;
                                                              }
                                                              .item {
                                                                  float: left;
                                                              }
                                                          
                                                      
                                                      
                                                          
                                                      1
                                                      2
                                                      3

                                                      9. position

                                                      • fixed
                                                      • relative
                                                      • absolute
                                                        9.1 fixed

                                                        返回顶部

                                                        .back {
                                                             position: fixed;
                                                             width: 60px;
                                                             height: 60px;
                                                             border: 1px solid red;
                                                             right: 50px;
                                                             bottom: 50px;
                                                         }
                                                        

                                                        对话框

                                                        
                                                        
                                                            
                                                            Document
                                                            
                                                                
                                                                body {
                                                                    margin: 0;
                                                                }
                                                                .dialog {
                                                                    position: fixed;
                                                                    height: 300px;
                                                                    width: 500px;
                                                                    background-color: white;
                                                                    left: 0;
                                                                    right: 0;
                                                                    margin: 0 auto;
                                                                    
                                                                    top: 200px;
                                                                    z-index: 1000;  /* 防止对话框被mask遮住 */
                                                                }
                                                                .mask {
                                                                    background-color: black;
                                                                    position: fixed;
                                                                    left: 0;
                                                                    right: 0;
                                                                    top: 0;
                                                                    bottom: 0;
                                                                    opacity: 0.7;
                                                                    z-index: 999;   /* 防止对话框被mask遮住 */
                                                                }
                                                            
                                                        
                                                        
                                                            

                                                        在这里插入图片描述

                                                        9.2 relative和absolute

                                                        在小米商城案例的基础上进行测试

                                                        https://blog.csdn.net/qq_43139145/article/details/...
                                                                .app{
                                                                    position: relative;
                                                                }
                                                                .app .download {
                                                                    position: absolute;
                                                                    display: none;
                                                                    height: 100px;
                                                                    width: 100px;
                                                                }
                                                                .app:hover .download {
                                                                    display: block;
                                                                }
                                                            
                                                        
                                                        
                                                            
                                                        https://blog.csdn.net/qq_43139145/article/details/...
                                                        

                                                        在这里插入图片描述

                                                        在这里插入图片描述

                                                        10. border

                                                        
                                                        
                                                            
                                                            Document
                                                            
                                                                .left {
                                                                    float: left;
                                                                }
                                                                .c1 {
                                                                    height: 200px;
                                                                    width: 300px;
                                                                    border: 3px dotted red;
                                                                    margin: 50px;
                                                                }
                                                                .c2 {
                                                                    height: 200px;
                                                                    width: 300px;
                                                                    border: 3px solid red;
                                                                    border-left: 3px solid green;
                                                                    margin: 50px;
                                                                }
                                                                .c3 {
                                                                    height: 200px;
                                                                    width: 300px;
                                                                    margin: 50px;
                                                                    background-color: bisque;
                                                                    border-left: 2px solid transparent;  /* 透明色 */
                                                                }
                                                                .c3:hover {
                                                                    border-left: 2px solid rgb(35, 211, 19);
                                                                }
                                                            
                                                        
                                                        
                                                            
                                                        我是虚线~
                                                        我是实线~左边框是绿色,上下右边框是红色
                                                        我是透明色,鼠标碰到我边框会变色哦~

                                                        在这里插入图片描述

                                                        11. 背景色

                                                        background-color: bisque;

                                                        无需多言☺


                                                        注意: 以上不是所有的CSS样式,这些是最常用的标签

                                                        4.案例: 小米商城

                                                        4.1 小米顶部

                                                        
                                                        
                                                            
                                                            小米商城
                                                            
                                                                /* 去掉body的边距 */
                                                                body {
                                                                    margin: 0;
                                                                }
                                                                .header {
                                                                    background-color: #333;
                                                                }
                                                                /* 让中间内容居中 */
                                                                .container {
                                                                    width: 1226px;
                                                                    margin: 0 auto;     /* 上下为0, 左右为auto */
                                                                }
                                                                /* header class 下的标签 a 自动应用这个样式 */
                                                                .header a {
                                                                    color: #b0b0b0;
                                                                    line-height: 40px;
                                                                    display: inline-block;
                                                                    font-size: 12px;
                                                                }
                                                                .header .menu {
                                                                    float: left;
                                                                    color: white;
                                                                }
                                                                .header .account {
                                                                    float: right;
                                                                    color: white;
                                                                }
                                                            
                                                        
                                                        
                                                            
                                                        
                                                        
                                                        

                                                        在这里插入图片描述

                                                        4.2 二级菜单

                                                        
                                                        
                                                            
                                                            小米商城
                                                            
                                                                /* 去掉body的边距 */
                                                                body {
                                                                    margin: 0;
                                                                }
                                                                .header {
                                                                    background-color: #333;
                                                                }
                                                                /* 让中间内容居中 */
                                                                .container {
                                                                    width: 1226px;
                                                                    margin: 0 auto;     /* 上下为0, 左右为auto */
                                                                }
                                                                /* header class 下的标签 a 自动应用这个样式 */
                                                                .header a {
                                                                    color: #b0b0b0;
                                                                    line-height: 40px;
                                                                    display: inline-block;
                                                                    font-size: 12px;
                                                                }
                                                                .header .menu {
                                                                    float: left;
                                                                    color: white;
                                                                }
                                                                .header a {
                                                                    text-decoration: none;
                                                                }
                                                                .header a:hover {
                                                                    color: white;
                                                                }
                                                                .header .account {
                                                                    float: right;
                                                                    color: white;
                                                                }
                                                                .sub-header {
                                                                    height: 100px;
                                                                }
                                                                .sub-header .hw {
                                                                    width: 234px;
                                                                    height: 100px;
                                                                }
                                                                .sub-header .logo {
                                                                    float: left;
                                                                }
                                                                /* a标签是行内标签,默认不支持设置高度与边距 因此设置padding是不起作用的,因此可以加上 inline-block */
                                                                .sub-header .logo a {
                                                                    padding-top: 22px;
                                                                    padding-bottom: 22px;
                                                                    display: inline-block;
                                                                }
                                                                /* 设置logo的图片像素大小 */
                                                                .sub-header .logo img {
                                                                    height: 56px;
                                                                    width: 56px;
                                                                }
                                                                .sub-header .menu {
                                                                    width: 400px;
                                                                    float:left;
                                                                    line-height: 100px;     /* 与行高度保持一致 */
                                                                }
                                                                .sub-header .menu a {
                                                                    text-decoration: none;  /* 去掉 a 标签的下划线 */
                                                                    color: #333;
                                                                    font-size: 16px;
                                                                    padding: 0 10px;        /* 设置字体的左右外边距 */
                                                                    display: inline-block;
                                                                }
                                                                /* 鼠标放到字体时,使字体变红 */
                                                                .sub-header .menu a:hover {
                                                                    color: #ff6700;
                                                                }
                                                                .sub-header .search {
                                                                    float: right;
                                                                }
                                                            
                                                        
                                                        
                                                            
                                                            
                                                        
                                                        
                                                        

                                                        在这里插入图片描述

                                                        4.3 推荐区域

                                                        
                                                        
                                                            
                                                            小米商城
                                                            
                                                                /* 去掉body的边距 */
                                                                body {
                                                                    margin: 0;
                                                                }
                                                                img {
                                                                    width: 100%;
                                                                    height: 100%;
                                                                }
                                                                .left {
                                                                    float: left;
                                                                }
                                                                .margin_left {
                                                                    margin-left: 14.5px;
                                                                }
                                                                .header {
                                                                    background-color: #333;
                                                                }
                                                                /* 让中间内容居中 */
                                                                .container {
                                                                    width: 1226px;
                                                                    margin: 0 auto;     /* 上下为0, 左右为auto */
                                                                }
                                                                /* header class 下的标签 a 自动应用这个样式 */
                                                                .header a {
                                                                    color: #b0b0b0;
                                                                    line-height: 40px;
                                                                    display: inline-block;
                                                                    font-size: 12px;
                                                                }
                                                                .header .menu {
                                                                    float: left;
                                                                    color: white;
                                                                }
                                                                .header a {
                                                                    text-decoration: none;
                                                                }
                                                                .header a:hover {
                                                                    color: white;
                                                                }
                                                                .header .account {
                                                                    float: right;
                                                                    color: white;
                                                                }
                                                                .sub-header {
                                                                    height: 100px;
                                                                }
                                                                .sub-header .hw {
                                                                    width: 234px;
                                                                    height: 100px;
                                                                }
                                                                .sub-header .logo {
                                                                    float: left;
                                                                }
                                                                /* a标签是行内标签,默认不支持设置高度与边距 因此设置padding是不起作用的,因此可以加上 inline-block */
                                                                .sub-header .logo a {
                                                                    padding-top: 22px;
                                                                    padding-bottom: 22px;
                                                                    display: inline-block;
                                                                }
                                                                /* 设置logo的图片像素大小 */
                                                                .sub-header .logo img {
                                                                    height: 56px;
                                                                    width: 56px;
                                                                }
                                                                .sub-header .menu {
                                                                    width: 400px;
                                                                    float:left;
                                                                    line-height: 100px;     /* 与行高度保持一致 */
                                                                }
                                                                .sub-header .menu a {
                                                                    text-decoration: none;  /* 去掉 a 标签的下划线 */
                                                                    color: #333;
                                                                    font-size: 16px;
                                                                    padding: 0 10px;        /* 设置字体的左右外边距 */
                                                                    display: inline-block;
                                                                }
                                                                /* 鼠标放到字体时,使字体变红 */
                                                                .sub-header .menu a:hover {
                                                                    color: #ff6700;
                                                                }
                                                                .sub-header .search {
                                                                    float: right;
                                                                }
                                                                .slider {
                                                                    height: 460px;
                                                                }
                                                                .news{
                                                                    margin-top: 14px;
                                                                }
                                                                .news .channel {
                                                                    width: 228px;
                                                                    height: 164px;
                                                                    background-color: #5f5750;
                                                                    padding: 3px;
                                                                }
                                                                .news .channel .item {
                                                                    width: 76px;
                                                                    height: 82px;
                                                                    float: left;
                                                                    text-align: center;
                                                                }
                                                                .news .channel .item img {
                                                                    width: 24px;
                                                                    height: 24px;
                                                                    display: block;         /* 让图片自已占一整行 */
                                                                    margin: 0 auto;         /* 让图片垂直居中 */
                                                                    margin-bottom: 4px;     /* 设置图片与下方字体的间距 */
                                                                }
                                                                .news .channel .item a {
                                                                    display: inline-block;
                                                                    font-size: 12px;        /* 设置字体大小 */
                                                                    text-decoration: none;  /* a标签去掉下划线 */
                                                                    padding-top: 18px;  
                                                                    color: #fff;          /* 设置字体为白色 */
                                                                    opacity: 0.7;           /* 设置透明度 */
                                                                }
                                                                .news .channel .item a:hover {
                                                                    opacity: 1;           /* 设置透明度 */
                                                                }
                                                                .news .list-item {
                                                                    width: 316px;
                                                                    height: 170px;
                                                                }
                                                            
                                                        
                                                        
                                                            
                                                            
                                                            
                                                        推荐商品

                                                        在这里插入图片描述

                                                        5. Bootstrap

                                                        别人已经帮忙写好的CSS样式

                                                        使用方式:

                                                        • 下载Bootstrap
                                                        • 使用:
                                                          • 在页面上引入 Bootstrap
                                                          • 编写HTML时,按照Bootstrap的规定来编写或者自定制

                                                            由于我没有下载Pycharm,无法本地实时测试,我使用的VSCode进行的编辑,所以我继续使用Flaskweb进行页面的访问测试

                                                            在这里插入图片描述

                                                            5.1 初识Bootstrap

                                                            下载地址: https://v3.bootcss.com/

                                                            在这里插入图片描述

                                                            在这里插入图片描述

                                                            BaiDuNetDisk Download:
                                                            链接:https://pan.baidu.com/s/1rcZldkNHrpC11f2plUv-rg?pwd=mh5b 
                                                            提取码:mh5b 
                                                            

                                                            下载完成后解压,目录如下:

                                                            在这里插入图片描述

                                                            在服务器中创建必要的目录

                                                            [root@hecs-33592 python]# cd bootstrap/
                                                            [root@hecs-33592 bootstrap]# ls
                                                            main.py  templates
                                                            [root@hecs-33592 bootstrap]# mkdir static
                                                            [root@hecs-33592 bootstrap]# cd static/
                                                            [root@hecs-33592 static]# ls
                                                            [root@hecs-33592 static]# mkdir css
                                                            [root@hecs-33592 static]# mkdir js
                                                            [root@hecs-33592 static]# mkdir img
                                                            [root@hecs-33592 static]# mkdir plugins
                                                            [root@hecs-33592 static]# tree /root/python/bootstrap/
                                                            /root/python/bootstrap/
                                                            ├── main.py
                                                            ├── static
                                                            │   ├── css
                                                            │   ├── img
                                                            │   ├── js
                                                            │   └── plugins
                                                            └── templates
                                                            

                                                            我会把刚刚下载好的bootstrap-3.4.1-dist.zip解压放到plugins下

                                                            [root@hecs-33592 plugins]# ls
                                                            bootstrap-3.4.1-dist.zip
                                                            [root@hecs-33592 plugins]# unzip bootstrap-3.4.1-dist.zip
                                                            [root@hecs-33592 plugins]# mv bootstrap-3.4.1-dist bootstrap-3.4.1
                                                            [root@hecs-33592 plugins]# tree bootstrap-3.4.1
                                                            bootstrap-3.4.1
                                                            ├── css
                                                            │   ├── bootstrap.css
                                                            │   ├── bootstrap.css.map
                                                            │   ├── bootstrap.min.css
                                                            │   ├── bootstrap.min.css.map
                                                            │   ├── bootstrap-theme.css
                                                            │   ├── bootstrap-theme.css.map
                                                            │   ├── bootstrap-theme.min.css
                                                            │   └── bootstrap-theme.min.css.map
                                                            ├── fonts
                                                            │   ├── glyphicons-halflings-regular.eot
                                                            │   ├── glyphicons-halflings-regular.svg
                                                            │   ├── glyphicons-halflings-regular.ttf
                                                            │   ├── glyphicons-halflings-regular.woff
                                                            │   └── glyphicons-halflings-regular.woff2
                                                            └── js
                                                                ├── bootstrap.js
                                                                ├── bootstrap.min.js
                                                                └── npm.js
                                                            

                                                            在这里插入图片描述

                                                            
                                                            
                                                                
                                                                Document
                                                                
                                                                
                                                                
                                                                
                                                            
                                                            
                                                                
                                                                
                                                                
                                                                
                                                                
                                                            
                                                            
                                                            

                                                            在这里插入图片描述

                                                            接下来使用已经写好的导航栏

                                                            链接地址: https://v3.bootcss.com/components/

                                                            在这里插入图片描述

                                                            在这里插入图片描述

                                                            复制上面的代码

                                                            
                                                            
                                                                
                                                                Document
                                                                
                                                                
                                                                
                                                                
                                                            
                                                            
                                                                
                                                                    
                                                                
                                                            
                                                            
                                                            

                                                            访问效果如下:

                                                            在这里插入图片描述

                                                            其实你仔细看会发现这个导航栏是有圆角的

                                                            接下来我们去掉圆角

                                                            F12调试页面

                                                            在这里插入图片描述

                                                            覆盖.navbar样式

                                                                .navbar {
                                                                    border-radius: 0;
                                                                }
                                                            
                                                            

                                                            再次访问就没有圆角了

                                                            可以在相应的位置进行修改,代码部分自己测试修改哈

                                                            在这里插入图片描述

                                                            5.2 栅格系统

                                                            栅格系统介绍

                                                            整体划分为了12格

                                                            大致分为四种风格

                                                            .col-xs-

                                                            .col-sm-

                                                            .col-md-

                                                            .col-lg-

                                                            在这里插入图片描述

                                                            响应式:根据页面的宽度,动态的改变布局

                                                            • .col-sm- : 750px
                                                            • .col-md- : 970px
                                                            • .col-lg- : 1170px

                                                              非响应式:

                                                              • .col-xs-

                                                                在这里插入图片描述

                                                                列偏移

                                                                col-sm-offset-

                                                                在这里插入图片描述

                                                                5.3 container

                                                                • container
                                                                  左边
                                                                  右边

                                                                  在这里插入图片描述

                                                                  • container-fluid
                                                                    左边
                                                                    右边

                                                                    在这里插入图片描述

                                                                    5.4 面板

                                                                    地址: https://v3.bootcss.com/components/#panels

                                                                    在这里插入图片描述

                                                                        
                                                                            
                                                                        
                                                                        
                                                                    左边
                                                                    Panel heading without title
                                                                    Panel content
                                                                    Panel heading without title
                                                                    Panel content
                                                                    Panel heading without title
                                                                    Panel content

                                                                    在这里插入图片描述

                                                                    5.5 媒体对象

                                                                    添加媒体对象

                                                                    在这里插入图片描述

                                                                    由于官方文档给的示例代码不全,所以可以F12查看源码,复制页面中的样式

                                                                    在这里插入图片描述

                                                                    Middle aligned media

                                                                    Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.

                                                                    Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

                                                                    Middle aligned media

                                                                    Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.

                                                                    Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

                                                                    Middle aligned media

                                                                    Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.

                                                                    Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

                                                                    Middle aligned media

                                                                    Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.

                                                                    Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

                                                                    在这里插入图片描述

                                                                    5.6 分页

                                                                    链接: https://v3.bootcss.com/components/#pagination

                                                                    在这里插入图片描述

                                                                    在这里插入图片描述

                                                                    案例: 登录

                                                                    在这里插入图片描述

                                            # First Name Last Name 操作
                                            1 Mark Otto 编辑 删除
                                            2 Jacob Thornton 编辑 删除
                                            3 Larry the Bird 编辑 删除

在这里插入图片描述

5.7 图标

上面的后台管理案例中,Bootstrap提供的图标不是太够用,我们需要一个专业做图标的网站

地址: https://fontawesome.dashgame.com/

下载

在这里插入图片描述

下载好后,上传至服务器的static/plugins下并解压

在这里插入图片描述

在这里插入图片描述

打开网址https://fontawesome.dashgame.com/

在这里插入图片描述

放在代码的这里

在这里插入图片描述

访问

在这里插入图片描述

以此类推,很简单

优化

针对前面的导航页面进行优化



    
    Document
    
    
    
    
    
    
        .distance {
            margin-left: 40px;
        } 
    


    
        
    
    

Middle aligned media

Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.

Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

2022-12-07 poker 1234

Middle aligned media

Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.

Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Middle aligned media

Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.

Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Middle aligned media

Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.

Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

最新推荐
Panel content
Panel heading without title
Panel content
Panel heading without title
Panel content

在这里插入图片描述

5.8 Bootstrap实现动态效果

依赖:

[root@hecs-33592 python]# cp -r bootstrap javascript

删除javascript中无用的html文件

[root@hecs-33592 python]# cd javascript/
[root@hecs-33592 javascript]# ls
main.py  static  templates
[root@hecs-33592 javascript]# cd templates/
[root@hecs-33592 templates]# ls
01.html  02.html  03.html  04.html  05.html
[root@hecs-33592 templates]# rm -rf ./*

首先编写一个小样例



    
    Document
    
        .menus {
            width: 200px;
            border: 1px solid red;
        }
        .menus .header {
            background-color: gold;
            padding: 20px 10px;
        }
    


    
    
        function myFunc() {
            alert("hello")
        }
    


访问测试

在这里插入图片描述

跳出对话框

在这里插入图片描述

更改,使用confirm

    function myFunc() {
        // alert("hello")
        confirm("是否要继续?")
    }

浏览器刷新访问

在这里插入图片描述

6.1 代码位置

js代码的存在形式:

海报
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon