滑块根据组合值显示不同的徽章。我有2个问题。
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Do I need a treat>!?</title>
<link rel="stylesheet" href="css/jquery-ui.css" type="text/css" media="screen">
<link rel="stylesheet" href="css/grid.css" type="text/css" media="screen">
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen">
<script src="js/jquery-1.10.2.min.js"></script>
<script src="js/jquery-ui.js"></script>
</head>
<body>
<p>
<label for="amount">Volume:</label>
<img id="amount-img" src="img/snackapp.png"/>
</p>
<style>
.slider { height: 200px; float:left; margin-right:20px; }
</style>
<div class="slider"></div>
<div class="slider"></div>
<div class="slider"></div>
<div class="slider"></div>
<div class="slider"></div>
<script>
$(function() {
$( ".slider" ).slider({
orientation: "vertical",
range: "min",
min: 0,
max: 100,
value: 0,
slide: function( event, ui ) {
var total = 0;
$('.slider').each( function() {
total += $(this).slider('value');
});
var img = 'snackapp.png';
if( total>=499) img = 'booze.png';
else if( total>=400 ) img = 'fishandchips.png';
else if( total>=300 ) img = 'pizza.png';
else if( total>=200 ) img = 'doughnut.png';
else if( total>=100 ) img = 'choc.png';
else if( total>=99 ) img = 'icecream.png';
else if( total<=98 ) img = 'fruit.png';
else if( total<=97 ) img = 'hotdrink.png';
;
$('#amount-img').attr( 'src', 'img/'+img );
}
});
});
</script>
</body>
</html>
答案 0 :(得分:0)
var img = 'snackapp.png';
似乎是默认图像,并且声明它的原因是等同于if语句的else子句。如果未满足if语句的任何条件,则img
仍为snackapp.png
。
每次移动滑块时,“徽章”更新都没有问题。您意识到滑块必须移动到某个阈值以使图像更改正确?图像正在根据连接在一起的所有滑块的总值而变化。
如果您不希望显示snackapp.png
,请使用您自己的默认图片进行更改,或将代码修改为
<强> HTML 强>
<p>
<label for="amount">Volume:</label>
<img id="amount-img" src="img/hotdrink.png" />
</p>
<div class="slider"></div>
<div class="slider"></div>
<div class="slider"></div>
<div class="slider"></div>
<div class="slider"></div>
<强> JS 强>
$(function () {
$(".slider").slider({
orientation: "vertical",
range: "min",
min: 0,
max: 100,
value: 0,
slide: function (event, ui) {
var total = 0;
$('.slider').each(function () {
total += $(this).slider('value');
});
var img = 'snackapp.png';
if (total >= 499) img = 'booze.png';
else if (total >= 400) img = 'fishandchips.png';
else if (total >= 300) img = 'pizza.png';
else if (total >= 200) img = 'doughnut.png';
else if (total >= 100) img = 'choc.png';
else if (total >= 99) img = 'icecream.png';
else if (total >= 50) img = 'fruit.png';
else if (total < 50) img = 'hotdrink.png';
$('#amount-img').attr('src', 'img/' + img);
}
});
});
将使热饮成为默认图像。