51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

jQuery插件实现带圆点的焦点图片轮播切换

这次分享的代码是jQuery插件,HovertreeImg是一个图片轮播jquery插件,使用方便,可以设置大小,圆点位置等,代码简洁


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <base target="_blank" />
  <meta charset="utf-8" />
  <style>#img {width:768px;height:66px;overflow:hidden}
#img img{width:100%;height:100%;}
#img #imgcontent{display:none}a{color:blue}</style>
</head>
<body>
  <div id="img">
    <a href="/h/bjaf/hovertreeimg.htm" title="Img" target="_blank"><img src="/jq/img/img.jpg" alt="Img插件" /></a>
    <div id="imgcontent">
      <a href="/h/bjaf/easysector.htm" title="HTML5百分比饼图" target="_blank"><img src="/themes/img/easysector.gif" alt="EasySector插件" /></a>
      <a href="/texiao/game/" title="见缝插针" target="_blank"><img src="/themes/img/jfcz.gif" alt="见缝插针" /></a>
     </div>
  </div>
  <div>
    <br /><br />
</div>

<script src="/ziyuan/jquery/jquery-1.12.0.min.js"></script> <script src="/jq/hovertreeimg/jquery.img.js"></script> <script> $("#img").hovertreeimg({ "h_circlePosition": "",//left,right,center "h_width": 768, "h_height": 66, "h_borderColor":"silver", "h_circleWidth": 14
}); </script> </body> </html>

jquery.img.js


/*!
* HovertreeImg(jQuery Plugin)
* version: 1.0.0
* Copyright (c) 2016 HoverTree 
*/
(function ($) {
  $.fn.hovertreeimg = function (options) {
var settings = $.extend({
  h_time:&quot;3000&quot;,//切换时间
  h_borderColor: &quot;transparent&quot;,//边框颜色
  h_width: &quot;500&quot;,//宽度
  h_height: &quot;200&quot;,//高度
  h_circleWidth: &quot;18&quot;,//方框边长
  h_circleColor:&quot;silver&quot;,//圆点颜色
  h_currentCircleColor: &quot;red&quot;,//当前圆点颜色
  h_circlePosition:&quot;right&quot;//圆点位置
}, options);

var h_hovertreeimg = $(this);
if (h_hovertreeimg.length &lt; 1)
  return;

h_hovertreeimg.css({
  &quot;position&quot;: &quot;relative&quot;, &quot;border&quot;:&quot;solid 1px &quot;+ settings.h_borderColor
  , &quot;width&quot;: settings.h_width, &quot;height&quot;: settings.h_height
  , &quot;overflow&quot;: &quot;hidden&quot;
})

var h_hovertreeimgcontent = h_hovertreeimg.find(&quot;&gt;div#hovertreeimgcontent&quot;);
h_hovertreeimgcontent.hide();

var h_hovertreeimgcurrent = h_hovertreeimg.find(&quot;&gt;a&quot;);
h_hovertreeimgcurrent.wrap(&quot;&lt;div id='replaceframe'&gt;&lt;/div&gt;&quot;);
h_replaceFrame = h_hovertreeimg.find(&quot;#replaceframe&quot;).css({ &quot;width&quot;: &quot;100%&quot;, &quot;height&quot;: &quot;100%&quot; });

//构造圆点框
$('&lt;div class=&quot;hovertreeimgpoint&quot;&gt;&lt;/div&gt;').appendTo(h_hovertreeimg);
var h_hovertreeimgpoint = h_hovertreeimg.find(&quot;.hovertreeimgpoint&quot;);

h_hovertreeimgcontent.prepend(h_hovertreeimgcurrent.clone(true));//复制到总a集合
var h_hovertreeimgitems = h_hovertreeimgcontent.children();//所有a标签集合
var h_hovertreeimglength = h_hovertreeimgitems.length;//所有轮播项数量
var h_isswitch = true;//是否轮播


var h_circleWidth = parseInt(settings.h_circleWidth);

//加边框与间隔
var h_circleFrameWidth = (h_circleWidth + 4) * h_hovertreeimglength+2;


h_hovertreeimgpoint.css({
  &quot;height&quot;: (h_circleWidth + 4), &quot;position&quot;: &quot;absolute&quot;, &quot;bottom&quot;: &quot;0px&quot;,
  &quot;display&quot;: &quot;inline-block&quot;
})
//设置圆点位置
switch (settings.h_circlePosition) {
  case 'right':
    h_hovertreeimgpoint.css({
      &quot;right&quot;: &quot;0px&quot;
    })
    break;
  case 'left':
    h_hovertreeimgpoint.css({
      &quot;left&quot;: &quot;0px&quot;
    })
    break;
  default:
    h_hovertreeimgpoint.css({
      &quot;left&quot;: &quot;0px&quot;,
      &quot;right&quot;: &quot;0px&quot;,
      &quot;width&quot;: h_circleFrameWidth + &quot;px&quot;,
      &quot;margin&quot;: &quot;0px auto&quot;
    })
    break;
}

//切换索引
var h_hovertreeimgindex = 1;
if (h_hovertreeimglength &lt; 2)
  h_hovertreeimgindex = 0;

//构造圆点
for (var h_i = 0; h_i &lt; h_hovertreeimglength; h_i++) {
  h_hovertreeimgpoint.append(&quot;&lt;div hovertreeimgdata='&quot; + h_i + &quot;' id='hovertreeimgpoint&quot; + h_i + &quot;'&gt;&lt;/div&gt;&quot;);
}
h_pointset = h_hovertreeimgpoint.find(&quot;div&quot;);//圆点集合
h_pointset.css({
  &quot;background-color&quot;: settings.h_circleColor, &quot;width&quot;: settings.h_circleWidth
  , &quot;height&quot;: settings.h_circleWidth
, &quot;border&quot;: &quot;1px solid white&quot;
  , &quot;margin-left&quot;: &quot;2px&quot;,
  &quot;display&quot;: &quot;inline-block&quot;,
  &quot;border-radius&quot;: &quot;50%&quot;
})
h_pointset.eq(0).css({ &quot;background-color&quot;: settings.h_currentCircleColor });


//设置当前图片
function imgswitch(imgindex) {
  h_replaceFrame.html(h_hovertreeimgitems.eq(imgindex));
  h_pointset.css({ &quot;background-color&quot;: settings.h_circleColor });
  h_pointset.eq(imgindex).css({ &quot;background-color&quot;: settings.h_currentCircleColor });
}

h_replaceFrame.find(&quot;img&quot;).css({
  &quot;width&quot;: settings.h_width
  , &quot;height&quot;: settings.h_height
})

//圆点操作
h_pointset.hover(function () {
  h_isswitch = false;//光标悬停到圆点停止切换
  imgswitch($(this).attr('hovertreeimgdata'));
}
, function () {
  h_isswitch = true;
}
)

//切换
setInterval(function () {
  if (!h_isswitch)
    return;
  imgswitch(h_hovertreeimgindex);
  h_hovertreeimgindex = (h_hovertreeimgindex + 1) % h_hovertreeimglength;
}, settings.h_time)

//光标悬停到图片停止切换
h_replaceFrame.hover(function () { h_isswitch = false; }, function () { h_isswitch = true; })

} }(jQuery));



赞(3)
未经允许不得转载:工具盒子 » jQuery插件实现带圆点的焦点图片轮播切换