_three_widgets_slider.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. $k++;
  17. echo $this->render('_partner_news_item',["model"=>$model->post,"key"=>$k]);
  18. }
  19. echo \yii\helpers\Html::endTag('div');
  20. echo \yii\helpers\Html::endTag('div');
  21. ?>
  22. <script>
  23. (function (){
  24. let shift = 0;
  25. const slider = document.getElementById('top_slider')
  26. let slides = Array.from(slider.getElementsByClassName('slides')[0].children)
  27. let width = (slides.length -1)* Math.ceil(slider.offsetWidth)-15;
  28. const buttons = slider.getElementsByClassName('arrows')[0]
  29. function next(){
  30. resetInterval()
  31. shift = shift - (Math.ceil(slider.offsetWidth) - 15);
  32. if(shift<(width*-1)){
  33. shift = 0;
  34. return slides.map((slide) => {
  35. slide.style.transform = "translate("+ shift +"px,0px)"
  36. })
  37. }
  38. slides.map((slide) => {
  39. slide.style.transform = "translate("+ shift +"px,0px)"
  40. })
  41. }
  42. function prev(){
  43. resetInterval()
  44. shift = shift + (Math.ceil(slider.offsetWidth) - 15);
  45. if(shift>0){
  46. shift = width*-1;
  47. return slides.map((slide) => {
  48. slide.style.transform = "translate("+ width*-1 +"px,0px)"
  49. })
  50. }
  51. slides.map((slide) => {
  52. slide.style.transform = "translate("+ shift +"px,0px)"
  53. })
  54. }
  55. if(typeof buttons !== 'undefined'){
  56. const arrows = Array.from(buttons.children)
  57. arrows.map((arrow) => {
  58. arrow.addEventListener('mousedown', e => {
  59. if(arrow.classList.contains('next')){
  60. next()
  61. } else {
  62. prev()
  63. }
  64. });
  65. })
  66. }
  67. let interval = setInterval(()=>next(),3000)
  68. const resetInterval = function()
  69. {
  70. clearInterval(interval);
  71. interval = setInterval(()=>next(),3000)
  72. }
  73. })()
  74. </script>