gpt4 book ai didi

javascript - 将颜色限制在 slider 位置

转载 作者:行者123 更新时间:2023-11-28 03:57:02 25 4
gpt4 key购买 nike

我用 JS 设计了一个 slider 问题,当 slider 向左移动位置时,我们将显示红色,中间我们将显示黄色,就像右边一样,我们将显示绿色,但是每当 slider 被移动,它显示为 slider 的总长度。我想将颜色限制在 slider 的位置。

$(document).ready(function(){
$('.slider_control').slider({
value:0,
range : 'min',
min: 0,
max: 100,
step: 1,
orientation: "horizontal",
change: function(e,ui){
var myid=$(this).parent().attr("id");
refreshSwatch(myid);
$(this).find("input").val(ui.value);
},
slide:function(e,ui){
var myid=$(this).parent().attr("id");
refreshSwatch(myid);
$(this).find("input").val(ui.value);
}
});
});
function getTheColor(colorVal){
var theColor = "";
if(colorVal<50){
myRed = 255;
myGreen = parseInt(((colorVal*2)*255)/100);
}
else {
myRed = parseInt(((100-colorVal)*2)*255/100);
myGreen = 255;
}
theColor = "rgb("+myRed+","+myGreen+",0)";
return(theColor);
}
function refreshSwatch(myid) {
var coloredSlider = $("#"+myid).find(".slider_control").slider("value");
console.log(coloredSlider);
myColor = getTheColor(coloredSlider);
//$(".slider_control.ui-slider-range").css("background-color",myColor);
$("#"+myid).find(".slider_control.ui-widget-content").css("background-color",myColor);
}

最佳答案

希望这有助于...

<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="//code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/ui-lightness/jquery-ui.css" />

<style type="text/css">

#custom-handle {
width: 3em;
height: 1.6em;
top: 50%;
margin-top: -.8em;
text-align: center;
line-height: 1.6em;
}

#slider {
background-color:#8c464f;
filter:progid:DXImageTransform.Microsoft.gradient(GradientType=1,startColorstr=#8c464f, endColorstr=#828c46);
background-image:-moz-linear-gradient(left, #8c464f 0%, #828c46 25%,#468c5f 100%);
background-image:linear-gradient(left, #8c464f 0%, #828c46 25%,#468c5f 100%);
background-image:-webkit-linear-gradient(left, #8c464f 0%, #828c46 25%,#468c5f 100%);
background-image:-o-linear-gradient(left, #8c464f 0%, #828c46 25%,#468c5f 100%);
background-image:-ms-linear-gradient(left, #8c464f 0%, #828c46 25%,#468c5f 100%);
background-image:-webkit-gradient(linear, left bottom, right bottom, color-stop(0%,#8c464f), color-stop(25%,#828c46),color-stop(100%,#468c5f));
}

.ui-widget-header{background-image:none;background-color:#FFFFFF;}
.ui-widget-content { background: #FFCCCC; }


</style>

<title>color slider </title>

</head>

<body>
<div id="slider" class="slider ">
<div id="custom-handle" class="ui-slider-handle"></div>
</div>

<script>
$( function() {

var handle = $( "#custom-handle" );
$( "#slider" ).slider({

range: "max",
step: 2,
animate: 0,


create: function() {
handle.text( $( this ).slider( "value" ) );
},
slide: function( event, ui ) {
handle.text( ui.value );
}
});


} );

</script>

关于javascript - 将颜色限制在 slider 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43407305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com