標籤:asp.net jquery ajax animate
.aspx代碼如下:
<%@ Page Title="" Language="C#" MasterPageFile="~/SAMPLE_CODE/AMST_SAMPLE.master" AutoEventWireup="true" CodeFile="S2_JAnimate_ImgMove.aspx.cs" Inherits="SAMPLE_CODE_S2_JAnimate_ImgMove" %>
<asp:Content ID="Content1" ContentPlaceHolderID="CPH_HEAD" Runat="Server">
<script src="../Jscript/jquery-1.8.3.js" type="text/javascript"></script>
<script type ="text/javascript">
//也可以設定一個定時器,定時移動圖片
var t;
$(document).ready(function () {
//頁面載入,啟動計時器
t = setInterval("$(‘#btn_r‘).click()", 3000);
//滑鼠移入層,出現切換的按鈕
$("#div_holder").mouseenter(function () {
$("#div_btns").fadeIn();
clearInterval(t);
});
$("#div_holder").mouseleave(function () {
$("#div_btns").fadeOut();
t = setInterval("$(‘#btn_r‘).click()", 3000);
});
//計算都是基於大圖尺寸而定的.
//點擊按鈕,移動內層
$("#btn_l").click(function () {
img_now = (img_now + img_count - 1) % img_count;//img_count由後台代碼計算得出, img_now數值是0-img_count-1。
doAnimate(img_now);
});
$("#btn_r").click(function () {
img_now = (img_now + 1) % img_count;
doAnimate(img_now);
});
});
//參數表示移動到第幾章圖片(0-6)
function doAnimate(_now) {
var lpx = -(_now * img_width) + "px";
$("#pnl_imgs").animate({ "opacity": 0.4 }, 400).animate({ "left": lpx }, 500).animate({ "opacity": 1 }, 400);
}
</script>
<%--
根據圖片高度和寬度的不同,計算和設定模組的CSS,
也可以通過JS代碼 或 LESS(CSS的擴充,在CSS代碼中引入JS進行計算)來設定相應控制項的高度
--%>
<style type ="text/css">
#div_holder {width:640px; height:360px; margin:0px auto; overflow:hidden; border:1px solid silver; }
#div_imgOuter { height:360px; /*高度必須設定,撐大外框層,確保左右按鈕位置不變*/
background-image:url(../imgs/loading.gif); background-position:center; background-repeat:no-repeat;
background-size:100% 100%;
position:relative; }
#div_btns { width:630px; margin:0px auto; display:none;
position:relative;
height:30px;
top:-195px; /*計算而得到的值 360/2+30/2 */ }
#pnl_imgs { position:relative;}
#btn_l,#btn_r
{ width:30px; height:30px; border:1px solid #777777;
border-radius:4px;
cursor:pointer;
background-image:url(../imgs/back_btn_1.jpg);
opacity:0.6; }
#btn_l:hover, #btn_r:hover
{ opacity:0.9;}
#btn_r { background-position:-30px 0px;}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="CPH_MEMO" Runat="Server">
<h3>實現圖片的切換,有2種方法</h3>
<ul>
<li>頁面載入時,所有圖片都載入到頁面中,通過位移動畫來切換</li>
<li>通過更改大圖的SRC地址,AJAX載入來切換</li>
<li>重點關注模組內元素的布局</li>
</ul>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="CPH_MAIN" Runat="Server">
<div id="div_holder"><%--整個模組的外框--%>
<div id="div_imgOuter" runat="server" clientidmode="Static"><%--裝載大圖的外框,其內層移動--%>
<asp:Panel ID="pnl_imgs" runat="server" ClientIDMode="Static"> <%--裝載大圖內層--%> </asp:Panel>
</div>
<div id="div_btns"><%--切換按鈕的層--%>
<b id="btn_l" class="fl"></b> <b id="btn_r" class="fr"></b>
</div>
</div>
<div class ="clr"></div>
</asp:Content>
.aspx.cs代碼如下:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.OleDb;public partial class SAMPLE_CODE_S2_JAnimate_ImgMove : System.Web.UI.Page{ string str_cnn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="; string str_sourcefile = "~/App_Data/DATA.mdb"; OleDbConnection cnn; OleDbCommand cmd; OleDbDataReader datar; string str_sql; public int img_width = 640; //這裡設定圖片的指定寬度;其他數值,均由此寬度決定 protected void Page_Load(object sender, EventArgs e) { //loadimgs,頁面載入,裝載圖片到層內 loadimgs(); } protected void loadimgs() { string str_conn = str_cnn + MapPath(str_sourcefile); cnn = new OleDbConnection(str_conn); cnn.Open(); str_sql = "select * from t_imgs"; cmd = new OleDbCommand(str_sql, cnn); datar = cmd.ExecuteReader(); Image _img; int imgcount = 0; while (datar.Read()) { _img = new Image(); _img.ImageUrl=datar["img_url"].ToString(); pnl_imgs.Controls.Add(_img); imgcount++; } //對於固定大小的大圖,計算出內層的大小 pnl_imgs.Style.Add("width", img_width * imgcount + "px"); //txt_imgs.Text = imgcount.ToString(); //txt_imgwidth.Text = img_width.ToString(); string js = "var img_count="+imgcount.ToString() + ";" + "var img_width =" + img_width.ToString() + ";" + "var img_now = 0;"; ScriptManager.RegisterStartupScript(this, this.GetType(), "", js, true); cnn.Close(); }}
如下:
aps.net--自動圖片移動模組( JQuery animate)