实现图片的轮播效果
1.显示页面:index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
var index = 0 ;
var imglength = ${list.size()}; //返回数组长度
$(".imgindex").eq(index).css("background","green"); //初始化第一张图片的背景
$.each($(".pic"),function(i,n){
$(n).css("left",400*i+"px");
})
setInterval(function(){
$.each($(".pic"),function(i,n){
$(n).animate({"left":parseInt($(n).css("left"))-400+"px"},1000,function(){
//当动画执行完成后执行的功能
if(i==0){
var $newimg = $(n).clone();
$(n).remove();
$newimg.css("left","800px");
$("#imgDiv").append($newimg);
}
});
})
index++;
if(index>imglength-1){
index=0;
}
$(".imgindex").css("background","red"); //初始化所有图片的默认背景
$(".imgindex").eq(index).css("background","green"); //改变指定图片index的背景色
},1500);
})
</script>
</head>
<body>
<div id="imgDiv" style="width:400px; height:200px;border:1px solid black; margin-left:100px; position:absolute;overflow:hidden;">
<c:forEach items="${list}" var="pic">
<img class="pic" src="${pic.path }" width="400" height="200" style="position:absolute;left:0px; top:0px;"/>
</c:forEach>
</div>
<div style=" position:absolute;top:180px;left:300px;">
<c:forEach begin="1" end="${list.size() }" var="step">
<%-- <span class="imgindex" style="background-color:red;">${step }</span>
--%>
<span class="imgindex" style="background-color:red;"><img src="images/dot.png"></img></span>
</c:forEach>
</div>
</body>
</html>
2.后台返回逻辑:
@Controller
public class PicController {
@Resource
private PicService picServiceImpl;
@RequestMapping("/")
public String welcome(Model model){
model.addAttribute("list",picServiceImpl.show());
return "/index.jsp";
}
}
原文地址:http://blog.51cto.com/59465168/2296231
时间: 2024-10-08 16:36:48