jquery实现商品按价格排序、按销量排序、随机排序(基于sort()方法实现)

前言: 三部分代码Ctrl C + V,一定可以实现效果。

部分一: 页面设计 ( 注: 底部导航栏用到了阿里的字体图标)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="stylesheet" href="css/main.css">
    <link rel="stylesheet" href="fonti/iconfont.css"> 
</head>
<body>
    <!--页面主体-->
    <div id="all">
        <!--页面头部-->
        <header>
            <button class="bt1" onclick="sortHandle('random')">随机</button>
            <button class="bt2" onclick="sortHandle('price')">价格</button>
            <button class="bt3" onclick="sortHandle('sales')">销量</button>
        </header>
        <!--页面主要内容-->
        <div id="main">
            <!-- <ul class="bookshow">
                <li><img src="imgtest/kris.jpg" alt=""></li>
                <li>
                    <p>东野圭吾:七年守护</p>
                </li>
                <li>
                    <font>(日)东野圭吾著:</font>
                </li>
                <li>价格:¥<span>29.5</span></li>
                <li>
                    <font>销量:</font>
                    <font>94</font>
                </li>
            </ul>
            -->
        </div>
        <!--页面底部-->
        <footer>
            <ul>
                <li class="iconfont">&#xe63c;</li>
                <li class="iconfont">&#xe686;</li>
                <li class="iconfont">&#xe501;</li>
                <li class="iconfont">&#xe51b;</li>
                
            </ul>
        </footer>
    </div>
    <script src="js/jquery-1.10.1.min.js"></script>
    <script src="js/myself.js"></script>
    </script>
</body>
</html>

部分二: 样式部分main.css

/*通用部分*/
li {
  list-style: none;
}

/*页面主体*/
#all {
  width: 100%;
  height: 100%;
  display: flex;
  flex-wrap: wrap;
  overflow-y: visible;
}
/*页面头部*/
header {
  width: 100%;
  height: 8%;
}
header button {
  width: 80px;
  height: 30px;
  margin: 1% 0 0 1%;
  background-color: firebrick;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
/*页面主要内容区*/
#main {
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  padding-bottom: 5%;
}
#main ul {
  width: 16%;
  margin-left: 1%;
}
#main ul li {
  width: 80%;
}
#main ul li img {
  width: 150px;
}
#main ul li p {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 18px;
}
#main ul li font {
  color: gray;
  font-size: 12px;
}
#main ul li:nth-child(2) {
  margin-top: -8%;
}
#main ul li:nth-child(3) {
  margin-top: -8%;
}
#main ul li:nth-child(4) {
  color: red;
}
/*页面底部*/
footer {
  width: 100%;
  height: 10%;
  position: fixed;
  bottom: 0px;
  background-color: white;
}
footer ul li {
  float: left;
  margin-left: 18%;
  font-size: 36px !important;
  cursor: pointer;
}

部分三: 逻辑实现,myself.js

//定义一个全局数组,用来存取我们从接口上获取的数据
var books = [];
//用来标记按价格排序按钮的点击次数
var p = 0;
//用来标记按销量排序的按钮的点击次数
var s = 0;

//页面初始化,默认顺序加载显示数据。并将从接口获取到的数据赋值给数组
$(function () {
  $.getJSON("http://api.cat-shop.penkuoer.com/books.json", function (data) {
    books = data;
    initHtml(data);
  });
});

//基于sort()方法排序实现对应条件排序
function sortHandle(type) {
  switch (type) {
    //随机排序
    case "random":
      //Math.random()随机取值是0-1;
      //Math.random() - 0.5的取值可能为正,也可能为负
      books.sort(function (a, b) {
        $("#main").empty();
        return Math.random() - 0.5;
      });
      break;
    //按价格排序
    case "price":
      //每点击一次,p自增一次
      p++;
      $("#main").empty();
      //点一次升序,再点一次降序(按价格排序同理)
      if (p % 2 == 0) {
        books.sort((a, b) => a.price - b.price);
      } else {
        books.sort((a, b) => b.price - a.price);
      }
      break;
    //按销量排序
    case "sales":
      s++;
      $("#main").empty();
      if (s % 2 == 0) {
        books.sort((a, b) => a.sales - b.sales);
      } else {
        books.sort((a, b) => b.sales - a.sales);
      }
      break;
    default:
      break;
  }
  initHtml();
}

/*往页面添加数据 */
function initHtml() {
  var str = "";
  books.forEach(function (book) {
    //以字符串模板的形式,遍历数组数据插入页面容器
    str = `
  <ul class="bookshow">
              <li><img src="${book.img}" alt=""></li>
              <li>
                  <p>${book.title}</p>
              </li>
              <li>
                  <font>${book.author}</font>
              </li>
              <li>价格:¥<span>${book.price}</span></li>
              <li>
                  <font>销量:</font>
                  <font>${book.sales}</font>
              </li>
      </ul>
  `;
    $("#main").append(str);
  });
}

///底部选中效果:选项卡效果的话我就不写了.累了感觉不会再爱了♥
$("footer ul li").click(function () {
  $(this).css("color", "firebrick").siblings().css("color", "");
  sortHandle("random");
});

页面效果:

在这里插入图片描述

结语: 我觉得逻辑实现部分,代码注释已经很详细了,就没有撰写那么多的文字叙述。
如果还有疑惑,请联系我。WeChat:CHEN1070036402

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的外卖系统 web 界面的 HTML 代码示例,包括排序、筛选和搜索功能: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>外卖系统</title> <style> /* 样式设置 */ .container { width: 1200px; margin: 0 auto; } .header { height: 80px; background-color: #ccc; } .menu { float: left; width: 20%; height: 600px; background-color: #eee; } .content { float: left; width: 80%; height: 600px; background-color: #fff; } .item { width: 30%; height: 200px; margin: 10px; float: left; border: 1px solid #ccc; padding: 10px; box-sizing: border-box; } .search { margin-top: 20px; } .search input { width: 200px; height: 30px; border: 1px solid #ccc; padding: 5px; font-size: 16px; box-sizing: border-box; } .search button { height: 32px; background-color: #f60; color: #fff; border: none; padding: 5px 20px; font-size: 16px; cursor: pointer; box-sizing: border-box; } </style> </head> <body> <div class="container"> <div class="header"> <!-- 头部导航栏 --> </div> <div class="menu"> <!-- 左侧菜单栏 --> <h3>菜系</h3> <ul> <li>川菜</li> <li>粤菜</li> <li>湘菜</li> </ul> <h3>口味</h3> <ul> <li>辣的</li> <li>酸的</li> <li>甜的</li> </ul> <h3>食材</h3> <ul> <li>鸡肉</li> <li>牛肉</li> <li>猪肉</li> </ul> </div> <div class="content"> <!-- 商品列表 --> <div class="search"> <!-- 搜索框 --> <input type="text" placeholder="请输入关键字"> <button>搜索</button> </div> <div class="sort"> <!-- 排序筛选 --> <label for="sort">排序:</label> <select id="sort"> <option value="price">价格</option> <option value="sales">销量</option> </select> <label for="filter">筛选:</label> <select id="filter"> <option value="cuisine">菜系</option> <option value="taste">口味</option> <option value="ingredient">食材</option> </select> <input type="text" placeholder="请输入筛选条件"> <button>筛选</button> </div> <div class="items"> <!-- 商品列表 --> <div class="item"> <h3>麻辣烫</h3> <p>销量:100</p> <p>价格:10元</p> </div> <div class="item"> <h3>红烧肉</h3> <p>销量:80</p> <p>价格:20元</p> </div> <div class="item"> <h3>宫保鸡丁</h3> <p>销量:120</p> <p>价格:15元</p> </div> </div> </div> </div> </body> </html> ``` 这个示例中,采用了一个简单的布局,包括一个顶部导航栏、一个左侧菜单栏、一个商品列表和一个搜索框。在商品列表中,使用了一个简单的排序和筛选功能,可以按照价格销量进行排序,以及按照菜系、口味和食材进行筛选。同时,搜索框可以根据关键字进行搜索。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值