jquery.img_silder.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. (function ($) {
  2. //调用方式 $('#silder').imgSilder({s_width:564, s_height:293, is_showTit:true, s_times:3000,css_link:'css/style.css'}); 容器必须加入 id silder_list or class silder_list
  3. /*参考结构
  4. <div class="silder" id="silder">
  5. <ul class="silder_list" id="silder_list">
  6. <li>
  7. <img src="css/img/1.jpg" border="0" alt="刘淇同志参观北京市志愿者之家">
  8. </li>
  9. <li>
  10. <img src="css/img/2.jpg" border="0" alt="刘淇同志与志愿者合影">
  11. </li>
  12. <li>
  13. <img src="css/img/3.jpg" border="0" alt="刘淇同志到北京大学人民医院调研">
  14. </li>
  15. <li>
  16. <img src="css/img/4.jpg" border="0" alt="2013中国志愿服务国际交流大会在京举行">
  17. </li>
  18. </ul>
  19. </div>
  20. */
  21. //获取当前js路径
  22. var __silderFile__ = $("script").last().attr("src");
  23. var __silderPath__=__silderFile__.substring(0,__silderFile__.lastIndexOf("/")+1);
  24. $.fn.silderDefaults = { //默认参数
  25. s_width:500, //容器宽度
  26. s_height:500, //容器高度
  27. is_showTit:true, // 是否显示图片标题 false :不显示,true :显示
  28. s_times:3000, //设置滚动时间
  29. css_link:__silderPath__+'css/style.css'
  30. };
  31. $.extendSilder = function (obj,opt) { //obj 元素对象,opt 参数对象
  32. var g = { //公共方法, 外部可调用
  33. //初始化
  34. init: function () {
  35. var wh ={width:opt.s_width,height:opt.s_height};
  36. var pagesize=0; //页码
  37. var silderList = $('#silder_list',g.obj);
  38. var silderList_li = $('#silder_list li',g.obj);
  39. g.LoadCSS(opt.css_link); //样式文件导入
  40. g.obj.css(wh); silderList.css(wh); silderList_li.find('img').css(wh); //设置宽高属性
  41. var currHtml = ""; //加入播放页码 及文字描述
  42. if(opt.is_showTit){ //判断是否显示标题
  43. currHtml += "<div class='silder_desc' id='silder_desc'></div>";
  44. }
  45. img_size = silderList_li.size() ;//图片个数
  46. currHtml += "<ul class='silder_page' id='silder_page'>";//分页码代码注入
  47. for(var i=0; i < img_size; i++){
  48. currHtml += "<li>"+ parseInt((1 + i),10) +"</li>";
  49. }
  50. currHtml +="</ul>";
  51. silderList_li.eq(0).show().siblings().hide(); //初始化隐藏其他图片
  52. g.obj.append(currHtml);//注入分页码
  53. var silderPage = $('#silder_page',g.obj);
  54. var silderPage_li =$('#silder_page li',g.obj);
  55. silderPage_li.eq(0).addClass('current');
  56. if(opt.is_showTit){ //初始化图片描述
  57. $('#silder_desc').text(silderList_li.eq(0).find('img').attr('alt'));
  58. }
  59. silderPage_li.on('click',function(){
  60. pagesize = $(this).index();
  61. silderList_li.eq(pagesize).fadeIn(1000).siblings().fadeOut(100);
  62. $(this).addClass('current').siblings().removeClass('current');
  63. if(opt.is_showTit){
  64. $('#silder_desc').text(silderList_li.eq(pagesize).find('img').attr('alt'));
  65. }
  66. });
  67. var t;
  68. silderList.hover(function(){window.clearInterval(t); return;},function(){ t = window.setInterval(function(){
  69. if(pagesize < img_size && pagesize >= 0)
  70. {
  71. silderList_li.eq(pagesize).fadeIn(1000).siblings().fadeOut(100);
  72. silderPage_li.eq(pagesize).addClass('current').siblings().removeClass('current');
  73. if(opt.is_showTit){
  74. $('#silder_desc').text(silderList_li.eq(pagesize).find('img').attr('alt'));
  75. }
  76. pagesize++;
  77. if(pagesize >= img_size){
  78. pagesize = 0;
  79. }
  80. }
  81. },opt.s_times);}).trigger("mouseout"); //悬浮时 停止自动动画,trigger 起默认触发作用
  82. },
  83. LoadCSS:function(url){ //新建css
  84. var s = document.createElement("LINK");
  85. s.rel = "stylesheet";
  86. s.type = "text/css";
  87. s.href = url;
  88. document.getElementsByTagName("HEAD")[0].appendChild(s);
  89. }
  90. };
  91. g.obj = $(obj);
  92. g.init();
  93. return g;
  94. }
  95. $.fn.imgSilder = function (options) {
  96. if (this.length == 0) return; //判断对象是否存在
  97. this.each(function () {
  98. if (this.usedSilder) return;
  99. var opt = $.extend({}, $.fn.silderDefaults, options); //合并已赋值参数
  100. this.usedSilder = $.extendSilder(this, opt);
  101. });
  102. }
  103. })(jQuery);
  104. $(function(){
  105. $('#silder').imgSilder({
  106. s_width:'100%', //容器宽度
  107. s_height:$('#silder').parent().height(), //容器高度
  108. is_showTit:true, // 是否显示图片标题 false :不显示,true :显示
  109. s_times:3000
  110. });
  111. });