jq编写弹幕功能

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title></title>
		<style type="text/css">
			#header {
				width: 100vw;
				height: 100vh;
				background-color: rgba(0,0,0,0.6);
				position: fixed;
				left: 0;
				top: 0;
			}

			.danMuItem {
				/* background-color: #333333; */
				/* color: #fff; */
				position: absolute;
				width: auto;
				white-space: nowrap;
				padding: 5px;
				border-radius: 10px;
				/* background-color: rgba(0, 0, 0, 0.5); */
				z-index: 2;
			}
			#header .danMuStyle0{
				color:#333;
				background-color:rgba(255,255,255,0.8)
			}
			#header .danMuStyle1{
				color:#ff5857;
				background-color:#3385ff
			}
			#header .danMuStyle2{
				color:#800080;
				background-color:#fa2121
			}
		</style>
	</head>
	<body>


		<div id="header">

		</div>


		<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
		<script type="text/javascript">
			$(function() {

				// 弹幕功能

				let arrlist = [],//弹幕数据
					weiZhi = [
						{
							top: "10",
							isOk: true,
							index: 0
						},
						{
							top: "65",
							isOk: true,
							index: 1
						},

						{
							top: "125",
							isOk: true,
							index: 2
						},

						{
							top: "200",
							isOk: true,
							index: 3
						}
					],//弹幕出现的位置
					danMuStyleNum=3,//样式种类数目
					sendIndex = undefined;//下一条弹幕的索引

				// 加载每行的Dom父节点
				$.each(weiZhi, (index, item) => {
					let $item = `
						<div class="danMu${index}"></div>
					`;
					$("#header").append($item);
					$($item).ready(function() {
						if (index == weiZhi.length - 1) {
							getList().then(resList => {
								arrlist = resList;
								gun();
							}).catch(() => {
								danMuTimer();
							})
						}
					})
				})
				// 获取两个数之间的随机整数
				function getRandom(min, max) {
					return Math.floor(Math.random() * (max - min+1)) + min
				}
				// 重新获取数据渲染
				function danMuTimer() {
					setTimeout(() => {
						getList().then(resList => {
							arrlist = resList;
							gun();
						}).catch(() => {
							danMuTimer();
						})
					}, 5000)
				}
				// 获取弹幕内容
				function getList() {
					return new Promise((resolve, reject) => {
						let list = [{
								name: "小明",
								prize: "兰博基尼一辆"
							},
							{
								name: "小红",
								prize: "卫龙辣条一包"
							},
							{
								name: "王富贵",
								prize: "一朵小红花"
							},
							{
								name: "老张",
								prize: "茅台一箱"
							}
						]; //模拟数据
						resolve(list)
					})
				}
				// 从第几行出来
				function weiZhiIndex() {
					let remainList = weiZhi.filter(item => item.isOk);
					if (remainList.length == 0) {
						return null
					} else {
						if (remainList.length == 1) {
							return remainList[0].index
						} else {
							return remainList[getRandom(0, remainList.length-1 )].index
						}
					}
				}
				// 开始滚动
				function gun() {
					setInterval(() => {
						let wei = weiZhiIndex();
						// console.log(wei)
						if (wei != null) {
							// console.log("可以")
							weiZhi[wei].isOk = false;

							if (sendIndex == undefined) {
								sendIndex = 0
							} else {
								sendIndex += 1;
								if (sendIndex > arrlist.length - 1) {
									// return danMuTimer();
									sendIndex = 0; //循环出现
								}
							}
							let item1 = arrlist[sendIndex];
							// let styleClass=styleList[getRandom(0,danMuStyleNum-1)];
							// console.log(getRandom(0,danMuStyleNum-1))
							let classNum=getRandom(0,danMuStyleNum-1);
							// console.log(classNum)
							let $item = $(
								`
									<div class="danMuItem danMuStyle${classNum}" data-wei="${wei}">
										恭喜${item1.name}获取到${item1.prize}
									</div> 
								`
							);
							// console.log($item)
							// console.log(sendIndex)
							$(`#header .danMu${wei}`).append($item);
							// console.log($item.width());
							$item.css({
								"left": $("#header").width() + "px",
								"top": weiZhi[wei].top + "px"
							});
							// console.log($item.width())
							let scrollTimer = setInterval(() => {
								$item.css({
									"left": $item.position().left - 1 + "px"
								});
								if ($item.position().left <= -$item.width()) {
									clearInterval(scrollTimer)
									$item.remove();
								}
								let weis = $item.attr("data-wei");

								if (($item.index() == $(`#header .danMu${weis}`).children().length - 1) && $("#header").width() - $item.position()
									.left - $item.width() >= getRandom(40, 60)) {
									weiZhi[weis].isOk = true;
								}
								if (($item.index() == $(`#header .danMu${weis}`).children().length - 1) && $item.position().left <= -
									$item.width()) {
									// weiZhi[weis].isOk = true;				
									clearInterval(scrollTimer)
									$item.remove();
								}
							}, 50)
						}
					}, 3000)
				}
			})
		</script>

	</body>
</html>

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
假设你有一个商品列表,每个商品都有名称、价格和库存数量。你可以使用 jq 编写一个简单的购物车程序来处理用户的购买请求。以下是一个可能的实现: ```bash # 商品列表,格式为 JSON 数组 products='[ {"name": "apple", "price": 2.5, "quantity": 10}, {"name": "banana", "price": 1.5, "quantity": 5}, {"name": "orange", "price": 3, "quantity": 8}, {"name": "pear", "price": 2, "quantity": 12} ]' # 定义一个空的购物车,格式为 JSON 对象 cart='{"items": []}' # 处理用户输入,假设用户输入的是商品名称和购买数量,格式为 "name:quantity" input="apple:3" # 解析用户输入,获取商品名称和购买数量 name=$(echo "$input" | cut -d':' -f1) quantity=$(echo "$input" | cut -d':' -f2) # 查找商品列表中对应的商品 product=$(echo "$products" | jq ".[] | select(.name == \"$name\")") # 检查商品是否存在以及库存是否足够 if [ -z "$product" ]; then echo "Product not found" elif [ "$(echo "$product" | jq '.quantity')" -lt "$quantity" ]; then echo "Not enough stock" else # 将商品添加到购物车中 item=$(echo "$product" | jq ".quantity = $quantity") cart=$(echo "$cart" | jq ".items += [$item]") echo "Item added to cart" fi # 打印购物车内容 echo "$cart" | jq ``` 这个程序使用 jq 命令来解析和操作 JSON 数据。首先定义了商品列表和购物车的初始值,然后处理用户输入,并在商品列表中查找对应的商品。如果商品存在且库存足够,将商品添加到购物车中。最后打印购物车内容。你可以根据需要扩展这个程序,例如支持删除购物车中的商品、计算购物车总价等等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值