第170天:面向对象-产品详情页开发

简介: 产品详情页开发思路产品详情页分为产品和和购物车2个对象1、产品首先分析产品的属性    产品属性有:产品名称、产品价格、团购价、购买数量、产品描述、产品图片等其次绑定产品信息  (1)绑定基本信息(bindBasic)     $('#pname').

产品详情页开发思路

产品详情页
分为产品和和购物车2个对象

1、产品

  • 首先分析产品的属性

    产品属性有:产品名称、产品价格、团购价、购买数量、产品描述、产品图片等

  • 其次绑定产品信息

  (1)绑定基本信息(bindBasic
    $('#pname').html(this.name);
    $('#price').html(this.price);
    ...
  (2)绑定图片列表(bindImages
    找产品图片部分的html代码
    拼接字符串
    var str='';
    将动态添加的部分改为变量形式 ,注意逗逗加加('+变量+')
    拼接完后将字符串添加到对应的位置

2、购物车

  • 购物车的属性

    购物车产品个数、产品总价格、产品列表

  • 购物车的方法

  (1)结算 计算总价格
  (2)获取产品总数
  (3)绑定基本信息 个数+总价格
  (4)绑定产品列表
    找到代码,拼接字符串,添加到相应位置


3、index.js

  • 创建产品实例

  var product = new Product();
  设置product的属性值,图片采用数组存储
  绑定基本信息 product.bindBasic();
  绑定图片 product.bindImages();

  • 绑定事件

  给加入购物车按钮添加点击事件
  点击时,应该更新购物车,并重新绑定购物车,触发相应事件

  • 创建购物车实例

  设置购物车的属性值
  再绑定购物车基本信息、购物车里面的产品列表

下面是详细代码:

1、product.js

 1 /**
 2  * Created by Administrator on 2018/2/4.
 3  */
 4 
 5 function Product(){
 6     //名称
 7     this.name="";
 8     //原价
 9     this.price=0;
10     //团购价
11     this.groupPrice=0;
12     //数量
13     this.buySum=0;
14     //描述
15     this.description="";
16     //图片
17     this.images=[{small:'',big:''},{small:'',big:''},{small:'',big:''}];
18 }
19 
20 Product.prototype={
21     //绑定基本信息
22     bindBasic:function(){
23         $('#pname').html(this.name);
24         $('#price').html(this.price);
25         $('#groupPrice').html(this.groupPrice);
26         $('#buyCount').html(this.buySum);
27         $('#description').html(this.description);
28     
30     },
31     //绑定图片列表
32     bindImages:function(){
33         //拼接
34         var str='';
35         for(var i=0;i<this.images.length;i++){
36             str+='<li>';
37             str+='<img class="etalage_thumb_image" src="'+this.images[i].small+'" class="img-responsive" />';
38             str+='<img class="etalage_source_image" src="'+this.images[i].big+'" class="img-responsive" />';
39             str+='</li>';
40 
41         }
42 
43         $('.etalage').html(str);
44 
45         $('#etalage').etalage({
46             thumb_image_width: 250,
47             thumb_image_height: 300
48         });
49 
50     }
51 }

2、cart.js

 1 /**
 2  * Created by Administrator on 2018/2/4.
 3  */
 4 function Cart(){
 5     //购物车的产品个数
 6     this.sum=0;
 7     //总价格
 8     this.allPrice=0;
 9     //产品列表
10     this.products=[];
11 }
12 
13 Cart.prototype={
14     //结算 计算总价格
15     accountClose:function(){
16         var sum=0;
17         for(var i=0;i<this.products.length;i++){
18             sum+=this.product[i].price;
19         }
20         return sum;
21     },
22 
23     //获取产品总数
24     getSum:function(){
25         return this.products.length;
26     },
27 
28     //绑定基本信息 个数+总价格
29     bindBasic:function(){
30         //个数
31         $('.cartsum').html(this.getSum());
32         //总价格
33         $('.allPrice').html(this.accountClose());
34     },
35 
36     //绑定列表
37     bindList:function(){
38         var str='';
39         for(var i=0;i<this.products.length;i++){
40             str+='<div class="cart_box">';
41             str+='<div class="message">';
42             str+='<div class="alert-close"></div>';
43             str+='<div class="list_img"><img src="'+this.products[i].images.small+'" class="img-responsive" alt=""/></div>';
44             str+='<div class="cart_box">';
45         }
46 
47       $('.shopping_cart').html(str);
48 
49 
50 
51     }
52 
53 
54 
55 }

3、index.js

 1 /**
 2  * Created by Lenovo on 2016/1/3.
 3  */
 4 
 5 
 6 /* 使用对象 搭积木*/
 7 
 8 /*绑定产品*/
 9 
10 window.onload =function(){
11 
12     /*实例化一个对象:为什么只有一个实例:再次理解抽象和具体:类和实例的区别*/
13     var product =  new Product();
14     product.name='HM休闲服登山包2018';
15     product.description='今天心情好,很好,非常好,马上过年了,真的真的真的好开心!';
16     product.price=194;
17     product.groupPrice=180;
18     product.buySum=20000;
19     /*数据结构:变量 数组(分成两种) json字典*/
20     product.images=[
21         {small:'../images/s11.jpg',big:'../images/s11.jpg'},
22         {small:'../images/s12.jpg',big:'../images/s12.jpg'},
23         {small:'../images/s13.jpg',big:'../images/s13.jpg'}
24     ];
25 
26 
27     /*面向对象编程*/
28     /*使用*/
29 
30     //现在:先宏观思考怎么做,然后再写细节
31 
32     /*绑定基本信息*/
33     product.bindBasic();
34 
35     /*绑定图片*/
36     product.bindImages();
37 
38 
39 //绑定事件
40 $('#btnaddcart').click(function(){
41     //购物车新增一个产品
42     cart.products.push(product);
43     //更新购物车 - 重新绑定购物车
44     cart.bindBasic();
45     cart.bindList();
46     //滑动到最顶部
47     $(window).scrollTop(0);
48 });
49 
50 
51 
52     /*绑定购物车*/
53 
54     var cart = new Cart();
55     cart.sum=10;
56     cart.allprice=8000;
57 
58     //假设购物车中已经有三个产品
59     cart.products.push(product);
60     cart.products.push(product);
61     cart.products.push(product);
62 
63     //绑定基本信息
64     cart.bindBasic();
65     //绑定购物车里面的产品列表
66     cart.bindList();
67 }

运行效果:

 

相关文章
|
1月前
|
小程序
云开发电商小程序实战教程-详情页原型
云开发电商小程序实战教程-详情页原型
|
18天前
|
安全 JavaScript Java
海外短剧系统开发详情版丨短剧系统开发指南流程/案例设计/功能需求/源码教程
Developing a short drama system requires consideration of multiple aspects, including system functionality, technical architecture, and user experience. Here is a detailed guide to help you understand the necessary steps and considerations for developing such a system
|
30天前
|
Web App开发 存储 自然语言处理
推荐一款价值几万元的免费开源GPTs导航!还可自定义数据源做成通用导航站!
推荐一款价值几万元的免费开源GPTs导航!还可自定义数据源做成通用导航站!
|
30天前
|
安全 区块链
DAPP商城系统开发详情版/案例设计/需求功能/源码教程
Requirement Analysis * *: Understand customer needs, determine the functions and features of the DApp mall system, including user registration, product display, purchase process, payment function, order management, user evaluation
|
1月前
|
人工智能 DataWorks 数据可视化
心动基于阿里云DataWorks构建游戏行业通用大数据模型
心动游戏在阿里云上构建云原生大数据平台,基于DataWorks构建行业通用大数据模型,如玩家、产品、SDK、事件、发行等,满足各种不同的分析型应用的要求,如AI场景、风控场景、数据分析场景等。
334 1
|
1月前
|
开发者
解决低代码详情页无法获取数据的问题
解决低代码详情页无法获取数据的问题
|
2月前
|
移动开发 数据可视化 前端开发
开发了一款新产品!可视化试卷搭建平台
开发了一款新产品!可视化试卷搭建平台
25 1
|
5月前
|
缓存 监控 数据可视化
DataV 如何做适配
DataV 如何做适配
128 0
|
6月前
SAP 电商云 FooterNavigationComponent 的设计细节
SAP 电商云 FooterNavigationComponent 的设计细节
39 0
|
8月前
|
安全 Python
盲盒商城系统模型开发技术方案源代码详情
blind_boxes = get_blind_boxes() return render_template(&#39;index.html&#39;, blind_boxes=blind_boxes) else: return redirect(url_for(&#39;login&#39;))