btnbar.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. var btnbar = {
  2. canMove: true,
  3. picDiv: null,
  4. positionl: 0,
  5. picwidth: 170,
  6. maxWidth: NaN,
  7. fundata: {},
  8. init: function (arrdata) {
  9. if (!document.getElementById("btnbar")) {
  10. $("body").prepend(
  11. '<div class="btnbar"> <div class="btnbar_panel"> <div class="btnbar_left"></div> <div class="btnbar_items_panel"><div class="btnbar_items_panel_scroll"> </div></div><div class="btnbar_right"></div> </div></div>'
  12. );
  13. }
  14. var inhtml = "";
  15. for (var i = 0; i < arrdata.length; i++) {
  16. var item = arrdata[i];
  17. inhtml += '<div class="btnbar_item">' + item.name + "</div>";
  18. this.fundata[item.name] = item;
  19. }
  20. $(".btnbar_items_panel_scroll").html(inhtml);
  21. this.picDiv = $(".btnbar_items_panel_scroll");
  22. this.maxWidth = 800 - arrdata.length * (this.picwidth + 5);
  23. $(".btnbar_item").bind("click", function () {
  24. $(this).addClass("btnbar_item_select").siblings().removeClass("btnbar_item_select");
  25. var _text = $(this).text();
  26. btnbar.fundata[_text].click();
  27. });
  28. if (this.maxWidth > 0) {
  29. $(".btnbar_left").hide();
  30. $(".btnbar_right").hide();
  31. this.picDiv.animate({ left: this.maxWidth / 2 + "px" });
  32. } else {
  33. $(".btnbar_left").bind("click", function () {
  34. btnbar.moveToLeft();
  35. });
  36. $(".btnbar_right").bind("click", function () {
  37. btnbar.moveToRight();
  38. });
  39. }
  40. },
  41. //左移
  42. moveToLeft: function () {
  43. if (this.positionl >= 0) {
  44. return;
  45. }
  46. this.positionl += this.picwidth;
  47. if (this.positionl >= 0) {
  48. this.positionl = 0;
  49. }
  50. this.picDiv.animate({ left: this.positionl + "px" });
  51. },
  52. //右移
  53. moveToRight: function () {
  54. if (this.positionl <= this.maxWidth) {
  55. return;
  56. }
  57. this.positionl -= this.picwidth;
  58. if (this.positionl <= this.maxWidth) {
  59. this.positionl = this.maxWidth;
  60. }
  61. this.picDiv.animate({ left: this.positionl + "px" });
  62. },
  63. unSelect: function () {
  64. $(".btnbar_item").each(function () {
  65. $(this).removeClass("btnbar_item_select");
  66. });
  67. },
  68. };