介紹一款網站前臺圖片滾動插件之"switchable"

1、簡單介紹:jQuery.Switchable是一款整合了Tabs、Slide、Scrollable等常見UI組件的jQuery插件,在這裏,簡答說說他的Slide。像Tabs,在Jquery-UI和Jquery-EasyUI中也有Tabs。在個人博客中也使用到了,請看完整「信息發佈系統」系列,下面就是Jquery-EasyUI的Tabs。javascript

                       

                                                        下面 這些是使用Switchable實現的效果:php

                                              

           一、今天就說說前臺圖片滾動的「Slide」,你們應該並不陌生,由於大部分網站都用到了這種效果。以下:官方地址下載css

                                

          二、今天就帶着你們完成如上圖的功能,包括添加完圖片後,自動添加數字(一、二、三、4),導向相應的頁面。              html

              (1)項目結構以下:java

                                                               

                 (2)、首先得在Scripts文件夾下加入jquery.switchable[all].min.js。若是沒有的話,請留言。jquery

                 (3)、在Home/Index中View中:app

                       一、$("#trigger1").switchable("#slide1 > div > img", { effect: "scroll", speed: .2 }).autoplay(2).carousel();jsp

                               屬性的簡單介紹:ide

                                    effect:切換效果。內置效果有:default":最簡單的顯/隱效果"還有「fade」,也能夠本身手動添加效果學習

                                    speed:動畫的速度,單位是秒。默認值即700毫秒。

                                    delay:觸發延遲,單位是秒。默認值即100毫秒。

                                    easing:"swing":動畫的效果。

                                    circular:false,是否循環。

                                    vertical:是否縱向切換。

                                方法的簡答介紹:

                                    autoplay:是否自動播放。

                                    interval:自動播放間隔,單位是秒。默認值即3000毫秒。當插件的參數是數字時,就是配置這個屬性。例如:
                                                  $("trigger-container-selector").switchable("panel-selector").autoplay(1.8);

                                    autopause:經過該屬性能夠控制鼠標 mouseenter 進 panel 區域是否暫停動畫。mouseleave 後將自動恢復動畫。                                     

                       
@{
    ViewBag.Title = "主頁";
}
<link href="@Url.Content("~/Content/SwitchTabs.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.switchable[all].min.js")" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function () {
        $("#trigger1").switchable("#slide1 > div > img", { effect: "scroll", speed: .2 }).autoplay(2).carousel(); //圖片滾動
    });
    function SetVisable(index) {
        $(".nav_content li").eq(index).addClass("current").siblings().removeClass("current");
    }
</script>

<div id="slide1" class="slide-panel" style="border-style: none; width: 680px;">
    <div>
        @Html.Raw(Server.HtmlDecode(Html.Action("PictureRoll", "Extensions").ToHtmlString()))
    </div>
</div>
<div id="trigger1" class="slide-trigger" align="center">
    <!-- 自動建立 triggers -->
</div>


@using (Html.BeginForm("AddImage", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="photo" />   
    <input type="submit" value="添加" />
}


<div>
  
</div>
Home/View

                         二、由於有一個<input type="file" />上傳文件。因此得添加這種格式:{enctype = "multipart/form-data" },簡單解釋一下:是設置表單的MIME編碼。默認狀況,這個編碼格式是application/x-www-form-urlencoded,不能用於文件上傳;只有使用了multipart/form-data,才能完整的傳遞文件數據,進行下面的操做。

                         三、 @Html.Raw(Server.HtmlDecode(Html.Action("PictureRoll", "Extensions").ToHtmlString())),圖片這部分是在Extensions中添加了一個Action,渲染出來圖片。下面看看Extensions/PictureRoll中的內容,使用的是:XElement,進行XML的操做。讀取XML的數據,不介紹它的使用了,都能看懂。使用System.Xml.Linq.最後ViewBag.picture=picture。把圖片渲染出來。前臺進行接收。

                        
        private const string picturePath = "~/XML/Pic.xml";
        public ActionResult PictureRoll()
        {
            string picture = string.Empty;
            XElement xe = XElement.Load(Server.MapPath(picturePath));
            var query = from value in xe.Elements("img")
                        select value;
            foreach (var item in query)
            {
                picture += "<img src=\"" + item.Element("path").Value + "\" title=\"" + item.Element("title").Value + "\" alt=\"" + item.Element("title").Value + "\" />";
            }
            ViewBag.picture = picture;
            return View();
        }
PictureRoll Action
                       
@{
    Layout = null;
}
@ViewBag.picture
PictureRoll View

                         四、還得添加一個CSS。改變圖片和1234連接的樣式。

               
/* SwichTab圖片滾動 */

.slide-trigger{position:relative;top:-25px;width:465px;padding-right:5px;}.slide-trigger a{display:inline-block;margin-right:3px;width:16px;height:16px;line-height:16px;text-align:center;color:#d94b01;background-color:#fff5e1;border:1px solid #f47500;outline:none;overflow:hidden;text-decoration:none;}.slide-trigger a:hover{text-decoration:none;}.slide-trigger a.current{width:18px;height:18px;line-height:18px;font-weight:700;color:#FFF;background:url(/images/t-bg.png) repeat-x;}.slide-panel{position:relative;width:950px;height:240px;overflow:hidden;border:1px solid #B6D1E6;}.slide-panel div{position:absolute;}.slide-panel div img{display:block;width:950px;height:240px;}
#slide1 div { width:2010em; /* 設置足夠的寬度 */ }
#slide1 div img { float:left; }
/* SwichTab圖片滾動 */
SwichTab Css

                         五、如今運行代碼:出現以前介紹的效果了吧。

                                    

                 (4)添加:上傳圖片的功能。

                          一、遍歷上傳上來的文件,使用HttpPostedFileBase進行接收。注意:參數 photo得和前臺的name得一致。

                          二、判斷上傳文件的類型,防止一些惡意用戶,上傳木馬。

                          三、item.SaveAs(Server.MapPath("~/images/" + Path.GetFileName(item.FileName)));進行保存文件。

                          四、接下來又是XElement的使用,SetElementValue,爲節點進行賦值。最後也得保存!Save();

                          五、出現上圖效果!

                        
 private const string picturePath = "~/XML/Pic.xml";
        [HttpPost]
        public ActionResult AddImage(HttpPostedFileBase[] photo)
        {
            foreach (var item in photo)
            {
                if (item!=null)
                {
                    var extention = Path.GetExtension(item.FileName);
                    if (extention == ".aspx" || extention == ".html" || extention == ".exe" || extention == ".asp" || extention == ".jsp" || extention == ".js" || extention == ".htm" || extention == ".php")
                    {
                        return Content("<script>alert('不能上傳這類型文件')</script>");
                    }                 
   
                    item.SaveAs(Server.MapPath("~/images/" + Path.GetFileName(item.FileName)));
                    XElement xe = XElement.Load(Server.MapPath(picturePath));
                    XElement element = new XElement(XName.Get("img"));
                    element.SetElementValue("id", DateTime.Now.ToString("yyyyMMddhhmmssfff"));
                    element.SetElementValue("title", Path.GetFileName(item.FileName));
                    element.SetElementValue("path", "/images/" + Path.GetFileName(item.FileName));
                    xe.Add(element);
                    xe.Save(Server.MapPath(picturePath));                       
                }
            }
            return RedirectToAction("Index","Home");
        }
AddImage Action

   2、總結:一個圖片焦點輪換的效果的實現,若是對您有一點點幫助的話,右下角」推薦「支持一下,讓更多的朋友來交流學習!若有問題,請留言! 今天下午有中國男籃亞錦賽,得趕快回家看了。支持一下中國男籃吧。

相關文章
相關標籤/搜索