_three_widgets_slider.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @var $this \yii\web\View
  4. * @var $models \app\models\front\TopSlider[]
  5. */
  6. $k = 0;
  7. echo \yii\helpers\Html::beginTag('div',['id'=>"top_slider","class"=>"col-md-4"]);
  8. if(count($models)>1){
  9. echo \yii\helpers\Html::beginTag('div',['class'=>'arrows']);
  10. echo \yii\helpers\Html::tag('div','<svg class="icon icon-h-slider-prev"><use xlink:href="/svg/symbol/sprite-clear.svg#h-slider-prev"></use></svg>',['class'=>"prev"]);
  11. echo \yii\helpers\Html::tag('div','<svg class="icon icon-h-slider-prev"><use xlink:href="/svg/symbol/sprite-clear.svg#h-slider-next"></use></svg>',['class'=>"next"]);
  12. echo \yii\helpers\Html::endTag('div');
  13. }
  14. echo \yii\helpers\Html::beginTag('div',["class"=>"slides"]);
  15. foreach ($models as $model){
  16. if( $model ){
  17. $k++;
  18. echo $this->render('_partner_news_item',["model"=>$model->post,"key"=>$k]);
  19. }
  20. }
  21. echo \yii\helpers\Html::endTag('div');
  22. echo \yii\helpers\Html::endTag('div');
  23. ?>
  24. <script>
  25. (function (){
  26. let shift = 0;
  27. const slider = document.getElementById('top_slider')
  28. let slides = Array.from(slider.getElementsByClassName('slides')[0].children)
  29. let width = (slides.length -1)* Math.ceil(slider.offsetWidth)-15;
  30. const buttons = slider.getElementsByClassName('arrows')[0]
  31. function next(){
  32. resetInterval()
  33. shift = shift - (Math.ceil(slider.offsetWidth) - 15);
  34. if(shift<(width*-1)){
  35. shift = 0;
  36. return slides.map((slide) => {
  37. slide.style.transform = "translate("+ shift +"px,0px)"
  38. })
  39. }
  40. slides.map((slide) => {
  41. slide.style.transform = "translate("+ shift +"px,0px)"
  42. })
  43. }
  44. function prev(){
  45. resetInterval()
  46. shift = shift + (Math.ceil(slider.offsetWidth) - 15);
  47. if(shift>0){
  48. shift = width*-1;
  49. return slides.map((slide) => {
  50. slide.style.transform = "translate("+ width*-1 +"px,0px)"
  51. })
  52. }
  53. slides.map((slide) => {
  54. slide.style.transform = "translate("+ shift +"px,0px)"
  55. })
  56. }
  57. if(typeof buttons !== 'undefined'){
  58. const arrows = Array.from(buttons.children)
  59. arrows.map((arrow) => {
  60. arrow.addEventListener('mousedown', e => {
  61. if(arrow.classList.contains('next')){
  62. next()
  63. } else {
  64. prev()
  65. }
  66. });
  67. })
  68. }
  69. let interval = setInterval(()=>next(),3000)
  70. const resetInterval = function()
  71. {
  72. clearInterval(interval);
  73. interval = setInterval(()=>next(),3000)
  74. }
  75. })()
  76. </script>