admin管理员组

文章数量:1339770

i have this error in console, but i dont understand why

var mySwiper = new Swiper('.swiper-container', {
    speed: 400,
    loop: true,
    slidesPerView:1,
    calculateHeight: true,
    spaceBetween: 50,
    watchActiveIndex: true,
    prevButton: '.swiper-button-prev',
    nextButton: '.swiper-button-next'
});

  $('input[type=radio][name=radio]').change(function() {
    var indx = $(this).val();
    mySwiper.slideTo(indx,400,false);
});

screen shot of error

i have this error in console, but i dont understand why

var mySwiper = new Swiper('.swiper-container', {
    speed: 400,
    loop: true,
    slidesPerView:1,
    calculateHeight: true,
    spaceBetween: 50,
    watchActiveIndex: true,
    prevButton: '.swiper-button-prev',
    nextButton: '.swiper-button-next'
});

  $('input[type=radio][name=radio]').change(function() {
    var indx = $(this).val();
    mySwiper.slideTo(indx,400,false);
});

screen shot of error

Share Improve this question asked Nov 9, 2016 at 14:13 Sergey SmekodubSergey Smekodub 1021 gold badge1 silver badge13 bronze badges 1
  • What is your html ? – Weedoze Commented Nov 9, 2016 at 14:18
Add a ment  | 

6 Answers 6

Reset to default 9

Probably you have more then one swiper element - then use array to call slideTo:

mySwiper[0].slideTo(1);

You need to make "mySwiper" golabl so you can call it in other functions to declare a global variable you must remove "var" and just start typing the variable's name like this:

mySwiper = new Swiper('.swiper-container', {
    speed: 400,
    loop: true,
    slidesPerView:1,
    calculateHeight: true,
    spaceBetween: 50,
    watchActiveIndex: true,
    prevButton: '.swiper-button-prev',
    nextButton: '.swiper-button-next'
});

Then you can call it where ever you need

Try this code:

mySwiper[mySwiper.length - 1].slideTo(0, 1000);

Your code seems correct.

Your problem is certainly link to the load of the script.

Check that you correctly load JQuery

<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

AND

<script src="https://cdnjs.cloudflare./ajax/libs/Swiper/3.4.0/js/swiper.min.js"></script>

You should change the className of swiper like this:

mySwiper = new Swiper('.swiper-box', {
    speed: 400,
    loop: true,
    slidesPerView:1,
    calculateHeight: true,
    spaceBetween: 50,
    watchActiveIndex: true,
    prevButton: '.swiper-button-prev',
    nextButton: '.swiper-button-next'
});

mySwiper is not defined in your change function.

I guess if you check the value of mySwiper in your change function, it will be null as it hasn't been set in the contect of your function.

本文标签: javascriptmyswiperslideTo() is not a functionStack Overflow