jquery css實(shí)現(xiàn)流程進(jìn)度條
本文實(shí)例為大家分享了jquery css實(shí)現(xiàn)流程進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下
方案1:

方案2:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>流程進(jìn)度條</title>
<style type="text/css">
.div_home{
width: 100%;
height: 720px;
background: pink;
}
.div_button{
width: 100%;
background: rgba(249, 214, 81, 1);
text-align: center;
}
:root {
--progress_div-height: 100px;
--progress_div-width: 100%;
--progress_div-background: rgba(204,232,207,1);
--progress_line-top: 50px;
--progress_line-height: 4px;
--progress_node-height: 20px;
--progress_node-width: 20px;
--progress_node-top: -8px;
--progress_node-lineHeight: 20px;
--progress_text-heigth: 20px;
--progress_text-width: 120px;
--progress_text-top: -30px;
--progress_color-yes: rgba(40 ,200 ,252 ,1);
--progress_color-no: rgba(213 ,213 ,213 ,1);
}
.progress_div{
height: var(--progress_div-height);
width: var(--progress_div-width);
background: var(--progress_div-background);
text-align: center;
margin: auto 0;
}
/*灰條樣式*/
.progress_line_no{
position: relative;
top: var(--progress_line-top);
height: var(--progress_line-height);
background: var(--progress_color-no);
}
/*藍(lán)條樣式*/
.progress_line_yes{
height: var(--progress_line-height);
background: var(--progress_color-yes);
}
/*未激活節(jié)點(diǎn)樣式*/
.progress_node_no{
position: absolute;
border-radius: 100%;
width: var(--progress_node-width);
height: var(--progress_node-height);
top: var(--progress_node-top);
line-height: var(--progress_node-lineHeight);
background: var(--progress_color-no);
color: var(--progress_color-no);
}
/*已激活節(jié)點(diǎn)樣式*/
.progress_node_yes{
position: absolute;
border-radius: 100%;
width: var(--progress_node-width);
height: var(--progress_node-height);
top: var(--progress_node-top);
line-height: var(--progress_node-lineHeight);
background: var(--progress_color-yes);
color: var(--progress_color-yes);
}
/*節(jié)點(diǎn)文字*/
.progress_text{
position: absolute;
vertical-align: middle;
text-align: center;
width: var(--progress_text-width);
height: var(--progress_text-heigth);
top: var(--progress_text-top);
}
/*當(dāng)前激活節(jié)點(diǎn)標(biāo)記*/
.progress_node_currentActive{
}
</style>
</head>
<body>
<div class="div_home">
<div class="progress_div">
<div class="progress_line_no">
<div class="progress_line_yes">
<div>
<div class="progress_text">1</div>
</div>
<div>
<div class="progress_text">2</div>
</div>
<div>
<div class="progress_text">3</div>
</div>
<div class="progress_node_currentActive">
<div class="progress_text">4</div>
</div>
<div>
<div class="progress_text">5</div>
</div>
</div>
</div>
</div>
<div class="div_button">
<input type="button" οnclick="skipNode(-1)" value="上一步">
<input type="button" οnclick="skipNode(1)" value="下一步">
</div>
</div>
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(function(){
//傳入灰條長(zhǎng)度,傳入最后一個(gè)激活節(jié)點(diǎn)下標(biāo)
loadProgress(1000 ,2);
});
//上一步type=-1,下一步type=1
function skipNode(type){
var currentNum = 0;
var countNum = $('.progress_line_no > .progress_line_yes > div').length;
//獲取當(dāng)前激活節(jié)點(diǎn)的下標(biāo)
$('.progress_line_no > .progress_line_yes > div').each(function(i ,data){
if($(data).hasClass('progress_node_currentActive') == true){
currentNum = i;
}
});
//當(dāng)前為first,上一步無效;當(dāng)前為last,下一步無效
if((type == -1 && currentNum == 0) || (type == 1 && currentNum == countNum - 1)){
return;
}
//重新設(shè)置激活節(jié)點(diǎn)標(biāo)記
$('.progress_line_no > .progress_line_yes > div').each(function(i ,data){
$(data).removeClass();
if(type == -1 && currentNum - 1 == i){
$(data).addClass('progress_node_currentActive');
}
if(type == 1 && currentNum + 1 == i){
$(data).addClass('progress_node_currentActive');
}
});
//重新載入流程進(jìn)度條樣式(傳入原進(jìn)度條長(zhǎng)度)
loadProgress($('.progress_line_no').width());
}
//加載流程進(jìn)度條,inLineWidth進(jìn)度條長(zhǎng)度,inCurrentNum最后一個(gè)激活節(jié)點(diǎn)下標(biāo)(從0開始到length-1)
function loadProgress(inLineWidth ,inCurrentNum){
var countNum = $('.progress_line_no > .progress_line_yes > div').length;//總節(jié)點(diǎn)數(shù)
var currentNum;//當(dāng)前激活節(jié)點(diǎn)下標(biāo)
//當(dāng)前激活節(jié)點(diǎn)優(yōu)先級(jí):loadProgress()方法傳入為最高級(jí)別,其次是div上class="progress_node_currentActive",最后默認(rèn)0
if(inCurrentNum != undefined && inCurrentNum > -1 && inCurrentNum < countNum){
//傳入的節(jié)點(diǎn)正確取傳入的節(jié)點(diǎn)為當(dāng)前激活節(jié)點(diǎn)
currentNum = inCurrentNum;
} else {
//存入的節(jié)點(diǎn)不正確,根據(jù)節(jié)點(diǎn)上的progress_node_currentActive設(shè)置當(dāng)前激活節(jié)點(diǎn)
$('.progress_line_no > .progress_line_yes > div').each(function(i ,data){
if($(data).hasClass('progress_node_currentActive') == true){
currentNum = i;
}
});
}
if(currentNum == undefined){
//未傳入節(jié)點(diǎn)或傳入的節(jié)點(diǎn)不正確 且div上沒發(fā)現(xiàn)progress_node_currentActive標(biāo)識(shí),設(shè)置當(dāng)前激活節(jié)點(diǎn)為0
currentNum = 0;
}
var line_width_no = inLineWidth;//灰條長(zhǎng)度
var line_width_yes;//藍(lán)條長(zhǎng)度
var node_distance = line_width_no / (countNum - 1);//兩點(diǎn)間距
var node_mid_distance = node_distance / 2;//兩點(diǎn)中距(間距/2)
$('.progress_line_no').width(line_width_no + 'px');//設(shè)置灰條長(zhǎng)度
$('.progress_line_no').css('left' ,($('.progress_line_no').parent().width() - line_width_no) / 2 + 'px');//設(shè)置灰條相對(duì)于父級(jí)div居中偏移
//設(shè)置節(jié)點(diǎn)和文字
$('.progress_line_no > .progress_line_yes > div').each(function(i ,data){
$(data).removeClass();//移除所有樣式
//設(shè)置當(dāng)前激活節(jié)點(diǎn)為progress_node_currentActive
if(currentNum == i){
$(data).addClass('progress_node_currentActive');
}
if(i == 0){
//設(shè)置first節(jié)點(diǎn)
$(data).addClass('progress_node_yes').css('left' ,i * node_distance - ($(data).width() / 2) + 'px');
}else if(i <= currentNum){
//設(shè)置激活節(jié)點(diǎn)
$(data).addClass('progress_node_yes').css('left' ,i * node_distance - ($(data).width() / 2) + 'px');
}else{
//設(shè)置未激活節(jié)點(diǎn)
$(data).addClass('progress_node_no').css('left' ,i * node_distance - ($(data).width() / 2) + 'px');
}
//設(shè)置文字偏移位置
$(data).children().css('left' ,-($(data).children().width() / 2) + 10+'px');
});
/*方案1,計(jì)算藍(lán)條長(zhǎng)度
*/
line_width_yes = line_width_no * currentNum / (countNum - 1);
/*方案2,計(jì)算藍(lán)條長(zhǎng)度
if(currentNum == 0){
//first節(jié)點(diǎn)為progress_node_currentActive時(shí)藍(lán)條長(zhǎng)度
line_width_yes = node_mid_distance * 1;
}else if(currentNum == countNum - 1){
//last節(jié)點(diǎn)為progress_node_currentActive時(shí)藍(lán)條長(zhǎng)度
line_width_yes = node_mid_distance * (countNum - 1) * 2;
}else{
//中間節(jié)點(diǎn)為progress_node_currentActive時(shí)藍(lán)條長(zhǎng)度
line_width_yes = node_mid_distance * (currentNum * 2 + 1);
}
*/
//設(shè)置藍(lán)條長(zhǎng)度
$('.progress_line_yes').width( line_width_yes + 'px');
}
</script>
</body>
</html>
使用:
1.首先要引入一個(gè)jquery.js
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
2.CSS:
:root開始所有css(css基本上都使用的變量,改樣式只需要改:root里的變量值就行)
3.JS:
保留所有js方法
調(diào)用loadProgress(1000,2)方法,傳入進(jìn)度條長(zhǎng)度、最后一個(gè)激活節(jié)點(diǎn)下標(biāo)(0到節(jié)點(diǎn)的length-1)
186行設(shè)置了整體相對(duì)于父級(jí)div居中,自己看需求改一下就好
4.標(biāo)簽:
主要就是class="progress_line_no"的div里的所有元素,最里面的兩層div就是節(jié)點(diǎn),class="progress_text"的div是文字,它們的父級(jí)div是圓點(diǎn)
5.激活節(jié)點(diǎn)優(yōu)先級(jí)
loadProgress(width,index)方法傳入index為最高級(jí)別,其次是div上class="progress_node_currentActive",最后默認(rèn)0
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery彈出層插件popShow(改進(jìn)版)用法示例
這篇文章主要介紹了jQuery彈出層插件popShow(改進(jìn)版)用法,對(duì)比前文分析了popShow插件的使用方法,需要的朋友可以參考下2017-01-01
基于JQuery實(shí)現(xiàn)異步刷新的代碼(轉(zhuǎn)載)
JQuery,是輕量級(jí)的js庫,把繁瑣的js代碼封裝,使調(diào)用更簡(jiǎn)單,完成更多功能。同樣,封裝了js利用XMLHttpRequest實(shí)現(xiàn)的異步刷新.2011-03-03
jquery select多選框的左右移動(dòng) 具體實(shí)現(xiàn)代碼
這篇文章介紹了jquery實(shí)現(xiàn)select多選框的左右移動(dòng)的方法,有需要的朋友可以參考一下2013-07-07
jQuery+CSS3文字跑馬燈特效的簡(jiǎn)單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄猨Query+CSS3文字跑馬燈特效的簡(jiǎn)單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家看,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06
jQuery深拷貝Json對(duì)象簡(jiǎn)單示例
這篇文章主要介紹了jQuery深拷貝Json對(duì)象的簡(jiǎn)單實(shí)現(xiàn)方法,以簡(jiǎn)單示例形式分析了jQuery深拷貝的操作技巧,需要的朋友可以參考下2016-07-07
jQuery UI Dialog 創(chuàng)建友好的彈出對(duì)話框?qū)崿F(xiàn)代碼
jQuery UI Dialog是jQuery UI的彈出對(duì)話框組件,使用它可以創(chuàng)建各種美觀的彈出對(duì)話框;它可以設(shè)置對(duì)話框的標(biāo)題、內(nèi)容,并且使對(duì)話框可以拖動(dòng)、調(diào)整大小、及關(guān)閉;平常主要用來替代瀏覽囂自帶的alert、confirm、open等方法2012-04-04
jquery實(shí)現(xiàn)上下左右滑動(dòng)的方法
這篇文章主要介紹了jquery實(shí)現(xiàn)上下左右滑動(dòng)的方法,是jQuery特效中非常典型的應(yīng)用,需要的朋友可以參考下2015-02-02

