简体   繁体   English

jQuery UI图像滑块

[英]jQuery UI Image Slider

I have a question about my jQuery UI image slider. 我对jQuery UI图像滑块有疑问。 I like to add more than two images to change, but I don't know how. 我喜欢添加两个以上的图像进行更改,但我不知道如何更改。 What should I change to add more Images to change with the slider? 我应该更改什么以添加更多图像以使用滑块更改? And how can I center the image, at this time it is floated left. 以及如何使图像居中,这时图像向左浮动。 Thank you very much! 非常感谢你!

 $(function() { $("#slider1").slider({ min: 0, max: 2, step: 1, change: function(event, ui) { if (ui.value == 1) { $('#flask').attr('src', 'img/tweet1.png'); } else { $('#flask').attr('src', 'img/tweet2.png'); }; } }); }); 
 #slider1 { align-content: center; margin-left: 15em; margin-right: 15em; margin-top: 5em; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img src="img/tweet2.png" height="270" width="600" id="flask" /> <div id="slider1"> 

This will require specific naming of your images. 这将需要对图像进行特定命名。 If you can get the count of images from a serverside script, this would also be helpful as it could set the max. 如果您可以从服务器端脚本获取图像计数,则这也可能会有所帮助,因为它可以设置最大值。

 $(function() { // Set to the number of images you will have var myMax = 2; $("#slider1").slider({ min: 0, max: myMax, step: 1, change: function(event, ui) { // All images must be named correctly: tweetN.png where N is a number // if statement in case you want to use 0 position to start with no image if(ui.value > 0){ $('#flask').attr('src', 'img/tweet' + ui.value + '.png'); } } }); }); 
 #slider1 { align-content: center; margin-left: 15em; margin-right: 15em; margin-top: 5em; } 
 <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-2.1.1.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <img src="img/tweet2.png" height="270" width="600" id="flask" /> <div id="slider1"></div> 

If the images will have different names, you can also create an array and iterate each postion of the array via Slider. 如果图像的名称不同,则还可以创建一个数组,并通过Slider迭代该数组的每个位置。

Example

$(function() {
  var myImages = [
    "img/tweet1.png",
    "img/tweet2.png",
    "img/logo3.jpg",
    "img/splash4.gif"
  ];
  $("#slider1").slider({
    min: 0,
    max: (myImages.length - 1),
    step: 1,
    change: function(event, ui) {
      $('#flask').attr('src', myImages[ui.value]);
    }
  });
});

Lots of options. 很多选择。 Again, if you have access to Server-side scripting like PHP, you could perform an ajax call and get a list of the current images. 同样,如果您可以访问服务器端脚本(如PHP),则可以执行ajax调用并获取当前图像的列表。 This way, you just drop the images into one folder, and the script will just know they are there. 这样,您只需将图像拖放到一个文件夹中,脚本便会知道它们在其中。

Hope that helps. 希望能有所帮助。

maybe you should try to increment the "max" value and then to add as many if statements as images you have. 也许您应该尝试增加“ max”值,然后添加与图像一样多的if语句。 Here is an example: 这是一个例子:

$(function() {
  $("#slider1").slider({
    min: 0,
    max: 4,
    step: 1,
    change: function(event, ui) {
      if (ui.value == 1) {
        $('#flask').attr('src', 'img/tweet1.png');
      } 
      if (ui.value == 2) {
        $('#flask').attr('src', 'img/tweet2.png');
      };
      if (ui.value == 3) {
        $('#flask').attr('src', 'img/tweet2.png');
      };
    }
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM