2013-04-04 73 views
0

我使用的这个jQuery UI滑块从http://jqueryui.com/slider/#steps 这是一个简单的滑块,具有3个步骤(1,2和3)和一个显示图片的div元素(“id_1”)。所以我现在真正想要的是图片的变化取决于滑块的位置。所以在位置“1”时,它显示一张图片,并且只要我将滑块移动到位置2或3,该图片就会改变。我该怎么做?jQuery UI:当滑块位置发生变化时的动作

<!doctype html> 
<html lang="en"> 
<head> 
<meta charset="utf-8" /> 
<title>jQuery UI Slider - Snap to increments</title> 
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" /> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> 
<link rel="stylesheet" href="/resources/demos/style.css" /> 
<script> 
    $(function() { 
     $("#slider").slider({ 
      value: 100, 
      min: 1, 
      max: 3, 
      step: 1, 
      slide: function (event, ui) { 
       $("#amount").val("$" + ui.value); 
      } 
     }); 
     $("#amount").val("$" + $("#slider").slider("value")); 
    }); 
</script> 
</head> 
<body> 
<p> 
<label for="amount">Donation amount ($50 increments):</label> 
<input type="text" id="amount" style="border: 0; color: #f6931f; font-weight: bold;" /> 
</p> 

<div id="slider"></div> 
<div id="id_1" class="class_1"></div> 

</body> 
</html> 

在此先感谢!

回答

0

您只需要查看滑块的值,然后显示相应的图像。尝试是这样的:

slide: function (event, ui) { 
      if (ui.value == 1) { 
       $("#id_1")).attr("src", "image1.png"); 
      } else if (ui.value == 2) { 
       $("#id_1")).attr("src", "image2.png"); 
      } else if (ui.value == 3) { 
       $("#id_1")).attr("src", "image3.png"); 
      } 
     } 

,使ID_1的div img标签来代替:

<img id="id_1" class="class_1"></div> 
+0

谢谢! ui.value的东西正是我所需要的。完美的作品! – Oinobareion 2013-04-04 12:29:48

0

你的意思是这样this(这是一个小提琴)。

我创建了拥有你需要这样的

var images = [ 
"http://cvcl.mit.edu/hybrid/cat2.jpg", 
"http://www.helpinghomelesscats.com/images/cat1.jpg", 
"http://oddanimals.com/images/lime-cat.jpg" 
]; // replace these links with the ones pointing to your images 

然后将图像的阵列中,当滑块移动

slide: function (event, ui) { 
      $("#amount").val("$" + ui.value); 
      $("#id_1").html("<img src=\"" + images[ui.value-1] + "\" />"); // subtracting 1 as the arrays first index is 0 
     } 

并通过在页面加载数组的第一个图像定稿负载

$("#id_1").html("<img src=\"" + images[0] + "\" />"); // Load the first image on page load 
0

只能在幻灯片事件中执行此操作。

 <script> 
     $(function() { 
      $("#slider").slider({ 
       value: 100, 
       min: 1, 
       max: 3, 
       step: 1, 
       slide: function (event, ui) { 
        $("#amount").val("$" + ui.value); 
if(ui.value == 1) 
{ 
    $('#imgSource').attr('src','1.png'); 
} 
else if(ui.value == 2) 
    { 
     $('#imgSource').attr('src','2.png'); 
    } 

       } 
      }); 
      $("#amount").val("$" + $("#slider").slider("value")); 
     }); 
    </script> 
+0

只需在Div中添加一个带有id imgSource的img即可。 – vijay 2013-04-04 10:47:15

相关问题