Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. $(function() {
  2. // **********************************************************************************************************************************
  3. // SliderShow Widget
  4. // **********************************************************************************************************************************
  5. $.widget( "cem.slidershow", {
  6. version: "0.0.1",
  7. options: {
  8. canMove: true,
  9. isMoving: false,
  10. direction: ''
  11. },
  12. _slider: null,
  13. // the constructor
  14. _create: function(options){
  15.  
  16. this._on(this.element, {
  17. mousemove:this._doMouseMove,
  18. mouseout:this._doMouseOut
  19. });
  20.  
  21. this.element
  22. .toggleClass( 'cem-slidershow', true )
  23. // prevent double click to select text
  24. .disableSelection();
  25.  
  26. this._slider = $("<div>", {"class": "cem-slidershow-slider"}).prependTo(this.element);
  27.  
  28. var _self = this;
  29. this.element.children('img')
  30. .each(function() {
  31. var d = $(this).data(),
  32. f =$('<figure>', {"class": d.group + " cem-slidershow-item"});
  33. f.append(this);
  34. f.append('<figcaption><h3>Chienne de vie</h3><p><strong>De Hélène Choquette</strong></p></figcaption></figure>');
  35. _self._slider.append(f);
  36. });
  37.  
  38. this.option(this.element.data());
  39. },
  40. // called when changing options
  41. _refresh: function() {
  42. // trigger a callback/event
  43. this._trigger( "change", $.Event(), {element: this.element} );
  44. },
  45. _doMouseMove: function(e) {
  46. if(e.offsetX + e.target.offsetLeft > this.element.innerWidth() - this._slider.children().innerWidth()) {
  47. this.options.direction = 'left';
  48. this.options.canMove = true;
  49. this._moveSlider();
  50. } else if (e.offsetX + e.target.offsetLeft < this._slider.children().innerWidth()) {
  51. this.options.direction = 'right';
  52. this.options.canMove = true;
  53. this._moveSlider();
  54. }
  55. },
  56. _doMouseOut: function(e) {
  57. this.options.canMove = false;
  58. },
  59. _moveSlider: function() {
  60. if(!this.options.isMoving) {
  61. var _self = this,
  62. childWidth = this._slider.children().width(),
  63. leftMargin = parseInt(this._slider.css('margin-left'));
  64.  
  65. if(this.options.direction == 'left') { // if we need to move the slider to the left
  66. if(leftMargin > -childWidth * (this.options.nbItems - 4)) {
  67. this.options.isMoving = true;
  68. this._slider.animate( {
  69. marginLeft: '-=' + (childWidth + 20)
  70. }, 1000, function() {
  71. _self.options.isMoving = false;
  72. _self._moveSlider();
  73. });
  74. }
  75. } else if (this.options.direction == 'right') { // if we need to move the slider to the right
  76. if(leftMargin < 0) {
  77. this.options.isMoving = true;
  78. this._slider.animate( {
  79. marginLeft: '+=' + (childWidth + 20)
  80. }, 1000, function() {
  81. _self.options.isMoving = false;
  82. _self._moveSlider();
  83. });
  84. }
  85. }
  86. }
  87.  
  88. },
  89. // events bound via _on are removed automatically
  90. // revert other modifications here
  91. _destroy: function() {
  92.  
  93. this.element
  94. .toggleClass( 'cem-slidershow', false )
  95. .enableSelection()
  96. .append(this.element.find('img'))
  97. .find('.cem-slidershow-item')
  98. .remove();
  99. },
  100. // _setOptions is called with a hash of all options that are changing
  101. // always refresh when changing options
  102. _setOptions: function() {
  103. // _super and _superApply handle keeping the right this-context
  104. this._superApply( arguments );
  105. this._refresh();
  106. },
  107. // _setOption is called for each individual option that is changing
  108. _setOption: function( key, value ) {
  109. this._super( key, value );
  110. }
  111. });
  112. // **********************************************************************************************************************************
  113.  
  114. $('.cem-slidershow').slidershow();
  115.  
  116. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement