mysqli+ajax+php+jquery实现动画轮播图

10 篇文章 0 订阅

前端:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="css/test.css">
</head>
<body>
	<!-- 动画轮播 -->
	<a id="ban_a1" class="ban_a">上一页</a>
	<div id="ban_imgs">
		<div id="imgs">
		</div>
	</div>
	<a id="ban_a2" class="ban_a">下一页</a>
	
	<script src="js/jquery.min.js"></script>
	<script>
		//最后实现从数据库中获取
		$(document).ready(function(){
			var currentPage = 1;
			$("#ban_a1").click(function(){
				if(currentPage == 1){
					return;
				}else if(currentPage > 1){
					htmlobj = $.ajax({url:"php/getimg.php?currentPage="+(currentPage-1),async:false});
					console.log(htmlobj.responseText);
					var arr = JSON.parse(htmlobj.responseText);
					var pageInfo = JSON.parse(arr[arr.length-1]);
					var totalPage = Number(pageInfo.totalPage);
					console.log("totalPage"+totalPage);
					$("#imgs").animate({left:'+=200px'});
					currentPage -= 1;
					console.log("currentPage="+currentPage);
				}
				
			});
			$("#ban_a2").click(function(){
				htmlobj = $.ajax({url:"php/getimg.php?currentPage="+(currentPage+1),async:false});
				console.log(htmlobj.responseText);
				var arr = JSON.parse(htmlobj.responseText);
				var pageInfo = JSON.parse(arr[arr.length-1]);
				var totalPage = Number(pageInfo.totalPage);
				console.log("totalPage"+totalPage);
				if(currentPage == totalPage){
					return;
				}else if(currentPage < totalPage){
					$("#imgs").animate({left:'-=200px'});
					currentPage += 1;
					console.log("currentPage="+currentPage);
				}
			});
		});

		function getAllImg() {
			$(document).ready(function(){
				console.log("getPage11");
				htmlobj = $.ajax({url:"php/getAllImg.php",async:false});
				console.log(htmlobj.responseText);
				var arr = JSON.parse(htmlobj.responseText);
				str = "";
				for(var i=0;i<arr.length;i++){
					str += "<a href=\"\"><img src=\"" + arr[i].path + "\" id=\"ban_img1\" class=\"ban_img\"></a>";
					
				}
				$("#imgs").html(str);
			});
		}

		window.onload = function(){
			getAllImg();
		}
	</script>
</body>
</html>
test.css

#ban_imgs {
	width: 200px;
	height: 150px;
	position: relative; /* 必须要加 */
	overflow: hidden;
}
#imgs {
	position: absolute;
	width: 400px;  /* 必须加,不然影响用户体验 */
	font-size: 0;  /* 消除图片之间的空隙 */
	left: 0px;
}
.ban_a {
	cursor: pointer;
}
.ban_img {
	width: 100px;
	height: 150px;
}



后台:

init.php

<?php

	$conn = mysqli_connect("127.0.0.1","root","","test",3306);
	$initsql = "SET NAMES UTF8";
	mysqli_query($conn,$initsql);
?>
getAllImg.php

<?php

	require("init.php");

	$sql = "SELECT * FROM test_img";
	$result = mysqli_query($conn,$sql);

	if($result === false){
		echo "异常,请检查SQL语句";
		echo $sql;
	}else{
		$arr = mysqli_fetch_all($result,1);
		$arr = json_encode($arr);
		echo $arr;
	}
?>
getimg.php

<?php

	require("init.php");

	@$currentPage = $_REQUEST["currentPage"];
	if($currentPage == "" || $currentPage == null){
		$currentPage = 1;
	}
	@$pageSize = $_REQUEST["pageSize"];
	if($pageSize == "" || $pageSize == null){
		$pageSize = 2;
	}
	$sql = "SELECT count(*) FROM test_img";
	$result = mysqli_query($conn,$sql);
	if($result === false){
		echo "异常,请检查SQL语句";
		echo $sql;
	}else{
		$arr = mysqli_fetch_row($result);
		$rowCount = $arr[0];  //考虑rowCount=0?
		//echo "rowCount=" . $rowCount;
		//echo "pageSize=" . $pageSize;
		$totalPage = ceil($rowCount/$pageSize);
		if($currentPage < 1){
			$currentPage = 1;
		}
		if($currentPage > $totalPage){
			$currentPage = $totalPage;
		}
		//echo "currentPage=" . $currentPage;
		$start = ($currentPage - 1) * $pageSize;
		$sql = "SELECT * FROM test_img LIMIT $start,$pageSize";
		$result = mysqli_query($conn,$sql);
		if($result === false){
			echo "异常,请检查SQL语句";
			echo $sql;
		}else{
			$arr = mysqli_fetch_all($result,1);
			$lastStr = "{\"currentPage\":$currentPage,\"totalPage\":$totalPage}"; 
			Array_push($arr,$lastStr);  
			$arr = json_encode($arr);
			echo $arr;
		}
	}

	

?>

本程序在刚开始加载时效率比较低:采取加载了所有的图片的方式,如果轮播图不多,还好,如果很多,建议优化。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值