JQuery

2010年12月23日 星期四

看板娘的LOGO替換功能

為了這個小功能害我連續兩天睡眠不足,結果只有短短幾行
需要跟css搭配沒辦法直接讓大家套用,僅簡單的講一下原理


head標籤裡頭//匯入jquery&jquery-cookie
<script src="js/jquery.min.js" type="text/javascript" />
<script src="js/jquery-cookie.min.js" type="text/javascript" />
<script>
if($.cookie("bgurl")==null){
    //當cookie內bgurl為空時,設定預設圖片
    $("#logo").css("background","url('"+"圖片網址"+"')");
}else{
     //設定logo背景為cookie內的bgurl
    $("#logo").css("background","url('"+$.cookie("bgurl")+"')");
}
</script>

右側選單//將圖片網址存入cookie
<script>
function setCookie(c_name,value,expiredays)
{
   var exdate=new Date()
   exdate.setDate(exdate.getDate()+expiredays)
   document.cookie=c_name+ "=" +escape(value)+ 
   ";domain=tsunderecon.blogspot.com; path=/" +
   ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
function BgChange(bgurl){ 
    setCookie('bgurl',bgurl,365);
    window.location.href=window.location.href;
}
</script>
//html
<a href="#" onclick="BgChange('圖片網址')">Saber</a>、
<a href="#" onclick="BgChange('圖片網址')">蓮華</a>

沒有留言:

張貼留言