jquery-powerFloat.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /*
  2. * jquery.powerFloat.js
  3. * jQuery 万能浮动层插件
  4. * http://www.zhangxinxu.com/wordpress/?p=1328
  5. * by zhangxinxu
  6. * 2010-12-06 v1.0.0 插件编写,初步调试
  7. * 2010-12-30 v1.0.1 限定尖角字符字体,避免受浏览器自定义字体干扰
  8. * 2011-01-03 v1.1.0 修复连续获得焦点显示后又隐藏的bug
  9. 修复图片加载正则判断不准确的问题
  10. * 2011-02-15 v1.1.1 关于居中对齐位置判断的特殊处理
  11. * 2011-04-15 v1.2.0 修复浮动层含有过高select框在IE下点击会隐藏浮动层的问题,同时优化事件绑定
  12. */
  13. (function($) {
  14. $.fn.powerFloat = function(options) {
  15. return $(this).each(function() {
  16. var s = $.extend({}, defaults, options || {});
  17. var init = function(pms, trigger) {
  18. if (o.target && o.target.css("display") !== "none") {
  19. o.targetClear();
  20. }
  21. o.s = pms;
  22. o.trigger = trigger;
  23. };
  24. switch (s.eventType) {
  25. case "hover": {
  26. $(this).hover(function() {
  27. init(s, $(this));
  28. var numShowDelay = parseInt(s.showDelay, 10), hoverTimer;
  29. //鼠标hover延时
  30. if (numShowDelay) {
  31. if (hoverTimer) {
  32. clearTimeout(hoverTimer);
  33. }
  34. hoverTimer = setTimeout(function() {
  35. o.targetGet();
  36. }, numShowDelay);
  37. } else {
  38. o.targetGet();
  39. }
  40. }, function() {
  41. o.flagDisplay = false;
  42. o.targetHold();
  43. if (s.hoverHold) {
  44. setTimeout(function() {
  45. o.displayDetect();
  46. }, 200);
  47. } else {
  48. o.displayDetect();
  49. }
  50. });
  51. if (s.hoverFollow) {
  52. //鼠标跟随
  53. $(this).mousemove(function(e) {
  54. o.cacheData.left = e.pageX;
  55. o.cacheData.top = e.pageY;
  56. o.targetGet();
  57. return false;
  58. });
  59. }
  60. break;
  61. }
  62. case "click": {
  63. $(this).click(function(e) {
  64. if (o.flagDisplay && e.target === o.trigger.get(0)) {
  65. o.flagDisplay = false;
  66. o.displayDetect();
  67. } else {
  68. init(s, $(this));
  69. o.targetGet();
  70. if (!$(document).data("mouseupBind")) {
  71. $(document).bind("mouseup", function(e) {
  72. var flag = false;
  73. $(e.target).parents().each(function() {
  74. if (o.target && $(this).attr("id") == o.target.attr("id") ) {
  75. flag = true;
  76. }
  77. });
  78. if (s.eventType === "click" && o.flagDisplay && e.target != o.trigger.get(0) && !flag) {
  79. o.flagDisplay = false;
  80. o.displayDetect();
  81. }
  82. return false;
  83. }).data("mouseupBind", true);
  84. }
  85. }
  86. });
  87. break;
  88. }
  89. case "focus": {
  90. $(this).focus(function() {
  91. var self = $(this);
  92. setTimeout(function() {
  93. init(s, self);
  94. o.targetGet();
  95. }, 200);
  96. }).blur(function() {
  97. o.flagDisplay = false;
  98. setTimeout(function() {
  99. o.displayDetect();
  100. }, 190);
  101. });
  102. break;
  103. }
  104. default: {
  105. init(s, $(this));
  106. o.targetGet();
  107. $(document).unbind("mouseup");
  108. }
  109. }
  110. });
  111. };
  112. var o = {
  113. targetGet: function() {
  114. //一切显示的触发来源
  115. if (!this.trigger) { return this; }
  116. var attr = this.trigger.attr(this.s.targetAttr), target = this.s.target;
  117. switch (this.s.targetMode) {
  118. case "common": {
  119. if (target) {
  120. var type = typeof(target);
  121. if (type === "object") {
  122. if (target.size()) {
  123. o.target = target.eq(0);
  124. }
  125. } else if (type === "string") {
  126. if ($(target).size()) {
  127. o.target = $(target).eq(0);
  128. }
  129. }
  130. } else {
  131. if (attr && $("#" + attr).size()) {
  132. o.target = $("#" + attr);
  133. }
  134. }
  135. if (o.target) {
  136. o.targetShow();
  137. } else {
  138. return this;
  139. }
  140. break;
  141. }
  142. case "ajax": {
  143. //ajax元素,如图片,页面地址
  144. var url = target || attr;
  145. this.targetProtect = false;
  146. if (/(\.jpg|\.png|\.gif|\.bmp|\.jpeg)$/i.test(url)) {
  147. if (o.cacheData[url]) {
  148. o.target = $(o.cacheData[url]);
  149. o.targetShow();
  150. } else {
  151. var tempImage = new Image();
  152. o.loading();
  153. tempImage.onload = function() {
  154. var w = tempImage.width, h = tempImage.height;
  155. var winw = $(window).width(), winh = $(window).height();
  156. var imgScale = w / h, winScale = winw / winh;
  157. if (imgScale > winScale) {
  158. //图片的宽高比大于显示屏幕
  159. if (w > winw / 2) {
  160. w = winw / 2;
  161. h = w / imgScale;
  162. }
  163. } else {
  164. //图片高度较高
  165. if (h > winh / 2) {
  166. h = winh / 2;
  167. w = h * imgScale;
  168. }
  169. }
  170. var imgHtml = '<img class="float_ajax_image" src="' + url + '" width="' + w + '" height = "' + h + '" />';
  171. o.cacheData[url] = imgHtml;
  172. o.target = $(imgHtml);
  173. o.targetShow();
  174. };
  175. tempImage.onerror = function() {
  176. o.target = $('<div class="float_ajax_error">图片加载失败。</div>');
  177. o.targetShow();
  178. };
  179. tempImage.src = url;
  180. }
  181. } else {
  182. if (url) {
  183. if (o.cacheData[url]) {
  184. o.target = $('<div class="float_ajax_data">' + o.cacheData[url] + '</div>');
  185. o.targetShow();
  186. } else {
  187. o.loading();
  188. $.ajax({
  189. url: url,
  190. success: function(data) {
  191. if (typeof(data) === "string") {
  192. o.target = $('<div class="float_ajax_data">' + data + '</div>');
  193. o.targetShow();
  194. o.cacheData[url] = data;
  195. }
  196. },
  197. error: function() {
  198. o.target = $('<div class="float_ajax_error">数据没有加载成功。</div>');
  199. o.targetShow();
  200. }
  201. });
  202. }
  203. }
  204. }
  205. break;
  206. }
  207. case "list": {
  208. //下拉列表
  209. var targetHtml = '<ul class="float_list_ul">', arrLength;
  210. if ($.isArray(target) && (arrLength = target.length)) {
  211. $.each(target, function(i, obj) {
  212. var list = "", strClass = "", text, href;
  213. if (i === 0) {
  214. strClass = ' class="float_list_li_first"';
  215. }
  216. if (i === arrLength - 1) {
  217. strClass = ' class="float_list_li_last"';
  218. }
  219. if (typeof(obj) === "object" && (text = obj.text.toString())) {
  220. if (href = (obj.href || "javascript:")) {
  221. list = '<a href="' + href + '" class="float_list_a">' + text + '</a>';
  222. } else {
  223. list = text;
  224. }
  225. } else if (typeof(obj) === "string" && obj) {
  226. list = obj;
  227. }
  228. if (list) {
  229. targetHtml += '<li' + strClass + '>' + list + '</li>';
  230. }
  231. });
  232. } else {
  233. targetHtml += '<li class="float_list_null">列表无数据。</li>';
  234. }
  235. targetHtml += '</ul>';
  236. o.target = $(targetHtml);
  237. this.targetProtect = false;
  238. o.targetShow();
  239. break;
  240. }
  241. case "remind": {
  242. //内容均是字符串
  243. var strRemind = target || attr;
  244. this.targetProtect = false;
  245. if (typeof(strRemind) === "string") {
  246. o.target = $('<span>' + strRemind + '</span>');
  247. o.targetShow();
  248. }
  249. break;
  250. }
  251. default: {
  252. var objOther = target || attr, type = typeof(objOther);
  253. if (objOther) {
  254. if (type === "string") {
  255. //选择器
  256. if (/<.*>/.test(objOther)) {
  257. //纯粹字符串了
  258. o.target = $('<div>' + objOther + '</div>');
  259. this.targetProtect = false;
  260. } else if ($(objOther).size()) {
  261. o.target = $(objOther).eq(0);
  262. this.targetProtect = true;
  263. } else if ($("#" + objOther).size()) {
  264. o.target = $("#" + objOther).eq(0);
  265. this.targetProtect = true;
  266. } else {
  267. o.target = $('<div>' + objOther + '</div>');
  268. this.targetProtect = false;
  269. }
  270. o.targetShow();
  271. } else if (type === "object") {
  272. if (!$.isArray(objOther) && objOther.size()) {
  273. o.target = objOther.eq(0);
  274. this.targetProtect = true;
  275. o.targetShow();
  276. }
  277. }
  278. }
  279. }
  280. }
  281. return this;
  282. },
  283. container: function() {
  284. //容器(如果有)重装target
  285. var cont = this.s.container, mode = this.s.targetMode || "mode";
  286. if (mode === "ajax" || mode === "remind") {
  287. //显示三角
  288. this.s.sharpAngle = true;
  289. } else {
  290. this.s.sharpAngle = false;
  291. }
  292. //是否反向
  293. if (this.s.reverseSharp) {
  294. this.s.sharpAngle = !this.s.sharpAngle;
  295. }
  296. if (mode !== "common") {
  297. //common模式无新容器装载
  298. if (cont === null) {
  299. cont = "plugin";
  300. }
  301. if ( cont === "plugin" ) {
  302. if (!$("#floatBox_" + mode).size()) {
  303. $('<div id="floatBox_' + mode + '" class="float_' + mode + '_box"></div>').appendTo($("body")).hide();
  304. }
  305. cont = $("#floatBox_" + mode);
  306. }
  307. if (cont && typeof(cont) !== "string" && cont.size()) {
  308. if (this.targetProtect) {
  309. o.target.show().css("position", "static");
  310. }
  311. o.target = cont.empty().append(o.target);
  312. }
  313. }
  314. return this;
  315. },
  316. setWidth: function() {
  317. var w = this.s.width;
  318. if (w === "auto") {
  319. if (this.target.get(0).style.width) {
  320. this.target.css("width", "auto");
  321. }
  322. } else if (w === "inherit") {
  323. this.target.width(this.trigger.width());
  324. } else {
  325. this.target.css("width", w);
  326. }
  327. return this;
  328. },
  329. position: function() {
  330. var pos, tri_h = 0, tri_w = 0, cor_w = 0, cor_h = 0, tri_l, tri_t, tar_l, tar_t, cor_l, cor_t,
  331. tar_h = this.target.data("height"), tar_w = this.target.data("width"),
  332. st = $(window).scrollTop(),
  333. off_x = parseInt(this.s.offsets.x, 10) || 0, off_y = parseInt(this.s.offsets.y, 10) || 0,
  334. mousePos = this.cacheData;
  335. //缓存目标对象高度,宽度,提高鼠标跟随时显示性能,元素隐藏时缓存清除
  336. if (!tar_h) {
  337. tar_h = this.target.outerHeight();
  338. if (this.s.hoverFollow) {
  339. this.target.data("height", tar_h);
  340. }
  341. }
  342. if (!tar_w) {
  343. tar_w = this.target.outerWidth();
  344. if (this.s.hoverFollow) {
  345. this.target.data("width", tar_w);
  346. }
  347. }
  348. pos = this.trigger.offset();
  349. tri_h = this.trigger.outerHeight();
  350. tri_w = this.trigger.outerWidth();
  351. tri_l = pos.left;
  352. tri_t = pos.top;
  353. var funMouseL = function() {
  354. if (tri_l < 0) {
  355. tri_l = 0;
  356. } else if (tri_l + tri_h > $(window).width()) {
  357. tri_l = $(window).width() - tri_h;
  358. }
  359. }, funMouseT = function() {
  360. if (tri_t < 0) {
  361. tri_t = 0;
  362. } else if (tri_t + tri_h > $(document).height()) {
  363. tri_t = $(document).height() - tri_h;
  364. }
  365. };
  366. //如果是鼠标跟随
  367. if (this.s.hoverFollow && mousePos.left && mousePos.top) {
  368. if (this.s.hoverFollow === "x") {
  369. //水平方向移动,说明纵坐标固定
  370. tri_l = mousePos.left
  371. funMouseL();
  372. } else if (this.s.hoverFollow === "y") {
  373. //垂直方向移动,说明横坐标固定,纵坐标跟随鼠标移动
  374. tri_t = mousePos.top;
  375. funMouseT();
  376. } else {
  377. tri_l = mousePos.left;
  378. tri_t = mousePos.top;
  379. funMouseL();
  380. funMouseT();
  381. }
  382. }
  383. var arrLegalPos = ["4-1", "1-4", "5-7", "2-3", "2-1", "6-8", "3-4", "4-3", "8-6", "1-2", "7-5", "3-2"],
  384. align = this.s.position, alignMatch = false, strDirect;
  385. $.each(arrLegalPos, function(i, n) {
  386. if (n === align) {
  387. alignMatch = true;
  388. return;
  389. }
  390. });
  391. if (!alignMatch) {
  392. align = "4-1";
  393. }
  394. var funDirect = function(a) {
  395. var dir = "bottom";
  396. //确定方向
  397. switch (a) {
  398. case "1-4": case "5-7": case "2-3": {
  399. dir = "top";
  400. break;
  401. }
  402. case "2-1": case "6-8": case "3-4": {
  403. dir = "right";
  404. break;
  405. }
  406. case "1-2": case "8-6": case "4-3": {
  407. dir = "left";
  408. break;
  409. }
  410. case "4-1": case "7-5": case "3-2": {
  411. dir = "bottom";
  412. break;
  413. }
  414. }
  415. return dir;
  416. };
  417. //居中判断
  418. var funCenterJudge = function(a) {
  419. if (a === "5-7" || a === "6-8" || a === "8-6" || a === "7-5") {
  420. return true;
  421. }
  422. return false;
  423. };
  424. var funJudge = function(dir) {
  425. var totalHeight = 0, totalWidth = 0, flagCorner = (o.s.sharpAngle && o.corner) ? true: false;
  426. if (dir === "right") {
  427. totalWidth = tri_l + tri_w + tar_w + off_x;
  428. if (flagCorner) {
  429. totalWidth += o.corner.width();
  430. }
  431. if (totalWidth > $(window).width()) {
  432. return false;
  433. }
  434. } else if (dir === "bottom") {
  435. totalHeight = tri_t + tri_h + tar_h + off_y;
  436. if (flagCorner) {
  437. totalHeight += o.corner.height();
  438. }
  439. if (totalHeight > st + $(window).height()) {
  440. return false;
  441. }
  442. } else if (dir === "top") {
  443. totalHeight = tar_h + off_y;
  444. if (flagCorner) {
  445. totalHeight += o.corner.height();
  446. }
  447. if (totalHeight > tri_t - st) {
  448. return false;
  449. }
  450. } else if (dir === "left") {
  451. totalWidth = tar_w + off_x;
  452. if (flagCorner) {
  453. totalWidth += o.corner.width();
  454. }
  455. if (totalWidth > tri_l) {
  456. return false;
  457. }
  458. }
  459. return true;
  460. };
  461. //此时的方向
  462. strDirect = funDirect(align);
  463. if (this.s.sharpAngle) {
  464. //创建尖角
  465. this.createSharp(strDirect);
  466. }
  467. //边缘过界判断
  468. if (this.s.edgeAdjust) {
  469. //根据位置是否溢出显示界面重新判定定位
  470. if (funJudge(strDirect)) {
  471. //该方向不溢出
  472. (function() {
  473. if (funCenterJudge(align)) { return; }
  474. var obj = {
  475. top: {
  476. right: "2-3",
  477. left: "1-4"
  478. },
  479. right: {
  480. top: "2-1",
  481. bottom: "3-4"
  482. },
  483. bottom: {
  484. right: "3-2",
  485. left: "4-1"
  486. },
  487. left: {
  488. top: "1-2",
  489. bottom: "4-3"
  490. }
  491. };
  492. var o = obj[strDirect], name;
  493. if (o) {
  494. for (name in o) {
  495. if (!funJudge(name)) {
  496. align = o[name];
  497. }
  498. }
  499. }
  500. })();
  501. } else {
  502. //该方向溢出
  503. (function() {
  504. if (funCenterJudge(align)) {
  505. var center = {
  506. "5-7": "7-5",
  507. "7-5": "5-7",
  508. "6-8": "8-6",
  509. "8-6": "6-8"
  510. };
  511. align = center[align];
  512. } else {
  513. var obj = {
  514. top: {
  515. left: "3-2",
  516. right: "4-1"
  517. },
  518. right: {
  519. bottom: "1-2",
  520. top: "4-3"
  521. },
  522. bottom: {
  523. left: "2-3",
  524. right: "1-4"
  525. },
  526. left: {
  527. bottom: "2-1",
  528. top: "3-4"
  529. }
  530. };
  531. var o = obj[strDirect], arr = [];
  532. for (name in o) {
  533. arr.push(name);
  534. }
  535. if (funJudge(arr[0]) || !funJudge(arr[1])) {
  536. align = o[arr[0]];
  537. } else {
  538. align = o[arr[1]];
  539. }
  540. }
  541. })();
  542. }
  543. }
  544. //已确定的尖角
  545. var strNewDirect = funDirect(align), strFirst = align.split("-")[0];
  546. if (this.s.sharpAngle) {
  547. //创建尖角
  548. this.createSharp(strNewDirect);
  549. cor_w = this.corner.width(), cor_h = this.corner.height();
  550. }
  551. //确定left, top值
  552. if (this.s.hoverFollow) {
  553. //如果鼠标跟随
  554. if (this.s.hoverFollow === "x") {
  555. //仅水平方向跟随
  556. tar_l = tri_l + off_x;
  557. if (strFirst === "1" || strFirst === "8" || strFirst === "4" ) {
  558. //最左
  559. tar_l = tri_l - (tar_w - tri_w) / 2 + off_x;
  560. } else {
  561. //右侧
  562. tar_l = tri_l - (tar_w - tri_w) + off_x;
  563. }
  564. //这是垂直位置,固定不动
  565. if (strFirst === "1" || strFirst === "5" || strFirst === "2" ) {
  566. tar_t = tri_t - off_y - tar_h - cor_h;
  567. //尖角
  568. cor_t = tri_t - cor_h - off_y - 1;
  569. } else {
  570. //下方
  571. tar_t = tri_t + tri_h + off_y + cor_h;
  572. cor_t = tri_t + tri_h + off_y + 1;
  573. }
  574. cor_l = pos.left - (cor_w - tri_w) / 2;
  575. } else if (this.s.hoverFollow === "y") {
  576. //仅垂直方向跟随
  577. if (strFirst === "1" || strFirst === "5" || strFirst === "2" ) {
  578. //顶部
  579. tar_t = tri_t - (tar_h - tri_h) / 2 + off_y;
  580. } else {
  581. //底部
  582. tar_t = tri_t - (tar_h - tri_h) + off_y;
  583. }
  584. if (strFirst === "1" || strFirst === "8" || strFirst === "4" ) {
  585. //左侧
  586. tar_l = tri_l - tar_w - off_x - cor_w;
  587. cor_l = tri_l - cor_w - off_x - 1;
  588. } else {
  589. //右侧
  590. tar_l = tri_l + tri_w - off_x + cor_w;
  591. cor_l = tri_l + tri_w + off_x + 1;
  592. }
  593. cor_t = pos.top - (cor_h - tri_h) / 2;
  594. } else {
  595. tar_l = tri_l + off_x;
  596. tar_t = tri_t + off_y;
  597. }
  598. } else {
  599. switch (strNewDirect) {
  600. case "top": {
  601. tar_t = tri_t - off_y - tar_h - cor_h;
  602. if (strFirst == "1") {
  603. tar_l = tri_l - off_x;
  604. } else if (strFirst === "5") {
  605. tar_l = tri_l - (tar_w - tri_w) / 2 - off_x;
  606. } else {
  607. tar_l = tri_l - (tar_w - tri_w) - off_x;
  608. }
  609. cor_t = tri_t - cor_h - off_y - 1;
  610. cor_l = tri_l - (cor_w - tri_w) / 2;
  611. break;
  612. }
  613. case "right": {
  614. tar_l = tri_l + tri_w + off_x + cor_w;
  615. if (strFirst == "2") {
  616. tar_t = tri_t + off_y;
  617. } else if (strFirst === "6") {
  618. tar_t = tri_t - (tar_h - tri_h) / 2 + off_y;
  619. } else {
  620. tar_t = tri_t - (tar_h - tri_h) + off_y;
  621. }
  622. cor_l = tri_l + tri_w + off_x + 1;
  623. cor_t = tri_t - (cor_h - tri_h) / 2;
  624. break;
  625. }
  626. case "bottom": {
  627. tar_t = tri_t + tri_h + off_y + cor_h;
  628. if (strFirst == "4") {
  629. tar_l = tri_l + off_x;
  630. } else if (strFirst === "7") {
  631. tar_l = tri_l - (tar_w - tri_w) / 2 + off_x;
  632. } else {
  633. tar_l = tri_l - (tar_w - tri_w) + off_x;
  634. }
  635. cor_t = tri_t + tri_h + off_y + 1;
  636. cor_l = tri_l - (cor_w - tri_w) / 2;
  637. break;
  638. }
  639. case "left": {
  640. tar_l = tri_l - tar_w - off_x - cor_w;
  641. if (strFirst == "2") {
  642. tar_t = tri_t - off_y;
  643. } else if (strFirst === "6") {
  644. tar_t = tri_t - (tar_w - tri_w) / 2 - off_y;
  645. } else {
  646. tar_t = tri_t - (tar_h - tri_h) - off_y;
  647. }
  648. cor_l = tar_l + cor_w;
  649. cor_t = tri_t - (tar_w - cor_w) / 2;
  650. break;
  651. }
  652. }
  653. }
  654. //尖角的显示
  655. if (cor_h && cor_w && this.corner) {
  656. this.corner.css({
  657. left: cor_l,
  658. top: cor_t,
  659. zIndex: this.s.zIndex + 1
  660. });
  661. }
  662. //浮动框显示
  663. this.target.css({
  664. position: "absolute",
  665. left: tar_l,
  666. top: tar_t,
  667. zIndex: this.s.zIndex
  668. });
  669. return this;
  670. },
  671. createSharp: function(dir) {
  672. var bgColor, bdColor, color1 = "", color2 = "";
  673. var objReverse = {
  674. left: "right",
  675. right: "left",
  676. bottom: "top",
  677. top: "bottom"
  678. }, dirReverse = objReverse[dir] || "top";
  679. if (this.target) {
  680. bgColor = this.target.css("background-color");
  681. if (parseInt(this.target.css("border-" + dirReverse + "-width")) > 0) {
  682. bdColor = this.target.css("border-" + dirReverse + "-color");
  683. }
  684. if (bdColor && bdColor !== "transparent") {
  685. color1 = 'style="color:' + bdColor + ';"';
  686. } else {
  687. color1 = 'style="display:none;"';
  688. }
  689. if (bgColor && bgColor !== "transparent") {
  690. color2 = 'style="color:' + bgColor + ';"';
  691. }else {
  692. color2 = 'style="display:none;"';
  693. }
  694. }
  695. var html = '<div id="floatCorner_' + dir + '" class="float_corner float_corner_' + dir + '">' +
  696. '<span class="corner corner_1" ' + color1 + '>◆</span>' +
  697. '<span class="corner corner_2" ' + color2 + '>◆</span>' +
  698. '</div>';
  699. if (!$("#floatCorner_" + dir).size()) {
  700. $("body").append($(html));
  701. }
  702. this.corner = $("#floatCorner_" + dir);
  703. return this;
  704. },
  705. targetHold: function() {
  706. if (this.s.hoverHold) {
  707. var delay = parseInt(this.s.hideDelay, 10) || 200;
  708. this.target.hover(function() {
  709. o.flagDisplay = true;
  710. }, function() {
  711. o.flagDisplay = false;
  712. //鼠标移出检测是否hover trigger,以决定其显示与否
  713. setTimeout(function() {
  714. o.displayDetect();
  715. }, delay);
  716. });
  717. }
  718. return this;
  719. },
  720. loading: function() {
  721. this.target = $('<div class="float_loading"></div>');
  722. this.targetShow();
  723. this.target.removeData("width").removeData("height");
  724. return this;
  725. },
  726. displayDetect: function() {
  727. //显示与否检测与触发
  728. if (!this.flagDisplay) {
  729. this.targetHide();
  730. }
  731. return this;
  732. },
  733. targetShow: function() {
  734. o.cornerClear();
  735. this.flagDisplay = true;
  736. this.container().setWidth().position();
  737. this.target.show();
  738. if ($.isFunction(this.s.showCall)) {
  739. this.s.showCall.call(this.trigger, this.target);
  740. }
  741. return this;
  742. },
  743. targetHide: function() {
  744. this.flagDisplay = false;
  745. this.targetClear();
  746. this.cornerClear();
  747. if ($.isFunction(this.s.hideCall)) {
  748. this.s.hideCall.call(this.trigger);
  749. }
  750. this.target = null;
  751. this.trigger = null;
  752. this.s = {};
  753. this.targetProtect = false;
  754. return this;
  755. },
  756. targetClear: function() {
  757. if (this.target) {
  758. if (this.target.data("width")) {
  759. this.target.removeData("width").removeData("height");
  760. }
  761. if (this.targetProtect) {
  762. //保护孩子
  763. this.target.children().hide().appendTo($("body"));
  764. }
  765. this.target.unbind().hide();
  766. }
  767. },
  768. cornerClear: function() {
  769. if (this.corner) {
  770. //使用remove避免潜在的尖角颜色冲突问题
  771. this.corner.remove();
  772. }
  773. },
  774. target: null,
  775. trigger: null,
  776. s: {},
  777. cacheData: {},
  778. targetProtect: false
  779. };
  780. $.powerFloat = {};
  781. $.powerFloat.hide = function() {
  782. o.targetHide();
  783. };
  784. var defaults = {
  785. width: "auto", //可选参数:inherit,数值(px)
  786. offsets: {
  787. x: 0,
  788. y: 0
  789. },
  790. zIndex: 999,
  791. eventType: "hover", //事件类型,其他可选参数有:click, focus
  792. showDelay: 0, //鼠标hover显示延迟
  793. hideDelay: 0, //鼠标移出隐藏延时
  794. hoverHold: true,
  795. hoverFollow: false, //true或是关键字x, y
  796. targetMode: "common", //浮动层的类型,其他可选参数有:ajax, list, remind
  797. target: null, //target对象获取来源,优先获取,如果为null,则从targetAttr中获取。
  798. targetAttr: "rel", //target对象获取来源,当targetMode为list时无效
  799. container: null, //转载target的容器,可以使用"plugin"关键字,则表示使用插件自带容器类型
  800. reverseSharp: false, //是否反向小三角的显示,默认ajax, remind是显示三角的,其他如list和自定义形式是不显示的
  801. position: "4-1", //trigger-target
  802. edgeAdjust: true, //边缘位置自动调整
  803. showCall: $.noop,
  804. hideCall: $.noop
  805. };
  806. })(jQuery);