jquery进度条,使用jquery.form.js实现文件上传及进度条前端代码
在这个信息爆炸的时代,了解jquery进度条和使用jquery.form.js实现文件上传及进度条前端代码的重要性不言而喻。本文将为您带来全面的解读,助您在这个领域中游刃有余。
jquery 页面跳转动画效果
HTML结构
该页面切换特效的HTML结构使用一个<main>元素来作为页面的包裹元素,div.cd-cover-layer用于制作页面切换时的遮罩层,div.cd-loading-bar是进行ajax加载时的loading进度条。
<main>
<div class="cd-index cd-main-content">
<div>
<h1>Page Transition</h1>
<!-- your content here-->
</div>
</div>
</main>
<div class="cd-cover-layer"></div><!-- this is the cover layer-->
<div class="cd-loading-bar"></div><!-- this is the loading bar-->
CSS样式
该页面切换特效中使用body::before和body::after伪元素在页面切换过程中创建两个遮罩层来遮住页面内容。它们的定位是固定定位,高度等于50vh,宽度为100%。默认情况下,使用CSS transform属性将它们隐藏起来(translateY(-100%)/translateY(100%))。当用户切换页面的时候,这些元素被移动回视口当中(通过在<body>元素上添加.page-is-changing class)。
下面的图片演示了这个过程:
页面切换特效
body::after, body::before{
/* these are the 2 half blocks which cover the content once the animation is triggered*/
height: 50vh;
width: 100%;
position: fixed;
left: 0;
}
body::before{
top: 0;
transform: translateY(-100%);
}
body::after{
bottom: 0;
transform: translateY(100%);
}
body.page-is-changing::after, body.page-is-changing::before{
transform: translateY(0);
}
页面切换时,页面内容的淡入淡出效果是通过改变div.cd-cover-layer的透明度实现的。它覆盖了.cd-main-content元素,并具有相同的背景色,然后在<body>被添加.page-is-changing class的时候,将透明度从0修改为1。
Loading进度条使用.cd-loading-bar::before伪元素来制作。默认它被缩小(scaleX(0))和transform-origin: left center。当页面切换开始时它被使用scaleX(1)放大会原来的尺寸。
.cd-loading-bar{
/* this is the loading bar- visible while switching from one page to the following one*/
position: fixed;
height: 2px;
width: 90%;
}
.cd-loading-bar::before{
/* this is the progress bar inside the loading bar*/
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
transform: scaleX(0);
transform-origin: left center;
}
.page-is-changing.cd-loading-bar::before{
transform: scaleX(1);
}
特效中平滑的过渡效果使用CSS Transitions来实现。每一个动画元素都被添加了不同的transition-delay,以实现不同的元素动画顺序。
JAVASCRIPT
该页面切换特效中在链接上使用data-type="page-transition"属性,用于触发页面切换事件。当插件检测到用户点击事件,changePage()方法将被执行。
$('main').on('click','[data-type="page-transition"]', function(event){
event.preventDefault();
//detect which page has been selected
var newPage=$(this).attr('href');
//if the page is not animating- trigger animation
if(!isAnimating) changePage(newPage, true);
});
这个方法会触发页面切换动画,并通过loadNewContent()方法加载新内容。
function changePage(url, bool){
isAnimating= true;
// trigger page animation
$('body').addClass('page-is-changing');
//...
loadNewContent(url, bool);
//...
}
当新的内容被加载后,会替代原来<main>元素中的内容。.page-is-changing class被从body中移除,新加载的内容会被添加到window.history中(使用pushState()方法)。
function loadNewContent(url, bool){
var newSectionName='cd-'+url.replace('.html',''),
section=$('<div class="cd-main-content'+newSectionName+'"></div>');
section.load(url+'.cd-main-content>*', function(event){
// load new content and replace<main> content with the new one
$('main').html(section);
//...
$('body').removeClass('page-is-changing');
//...
if(url!= window.location){
//add the new page to the window.history
window.history.pushState({path: url},'',url);
}
});
}
为了在用户点击浏览器的回退按钮时触发相同的页面切换动画效果,插件中监听popstate事件,并在它触发时执行changePage()函数。
$(window).on('popstate', function(){
var newPageArray= location.pathname.split('/'),
//this is the url of the page to be loaded
newPage= newPageArray[newPageArray.length- 1];
if(!isAnimating) changePage(newPage);
});
如何设置jQuery中progressBar的进度条颜色和其背景颜色
$('#psp').css({'background':'green'});但是进度条和背景一个颜色,都是浅蓝的,能够显示当前的进步,但是不能区分进度条和背景的颜色,
我想区分进度条和北京的颜色
谁能帮助一下,谢谢。问题补充:还是不行,进度条和背景仍旧一个颜色。问题补充:可能我的问题还不是太明白,我把代码贴出来<br/><br/><div id="psp" class="easyui-progressbar" style="width:630px;"</div<br/><br/>function text(result){<br/>$('#psp div').css({'background':'LightBlue'});<br/>$('#psp').css({'background':'green'});<br/> var value=$('#psp').progressbar('getValue');<br/> if(value< 100){<br/> value+= Math.floor(Math.random()* 10);<br/>$('#psp').progressbar('setValue', value);<br/> if(value<100){<br/> setTimeout("text('"+result+"')",200);<br/>}<br/>}<br/><br/><br/>目前就是想把进度条和背景的颜色分开,但是这两个颜色一直是一样的,不知道我的代码是否由问题,请大家指正。问题补充:<div class="quote_title">housen1987写道</div><div class="quote_div">你看一下我给的效果图,是不是这个样子的,关键是<div id='psp' style='padding:2px;'这句话<br/></div><br/>------------------<br/><br/>问题补充:<div class="quote_title">housen1987写道</div><div class="quote_div">现在才看明白了,你用的jquery UI<br/><br/><pre name="code" class="java"><style
.ui-progressbar{
background: green;
padding:1px;}
.ui-progressbar-value{
background: LightBlue;}</style<script type="text/javascript"
$(function(){
</body</pre><br/><br/>按这个代码试试,jquery UI改变样式一般用CSS修改:<br/><br/>官方文档:<br/><div class="quote_title">引用</div><div class="quote_div">Sample markup with jQuery UI CSS Framework classes<br/><div class="ui-progressbar ui-widget ui-widget-content ui-corner-all"<br/><div style="width: 37%;" class="ui-progressbar-value ui-widget-header ui-corner-left"</div<br/></div<br/>Note: This is a sample of markup generated by the progressbar plugin, not markup you should use to create a progressbar. The only markup needed for that is<div</div.</div><br/></div><br/>-----------------<br/>style完全不起作用。
使用jquery.form.js实现文件上传及进度条前端代码
ajax的表单提交只能提交data数据到后台,没法实现file文件的上传还有展示进度功能,这里用到form.js的插件来实现,搭配css样式简单易上手,而且高大上,推荐使用。
需要解释下我的结构,#upload-input-file的input标签是真实的文件上传按钮,包裹form标签后可以实现上传功能,#upload-input-btn的button标签是展示给用户的按钮,因为需要样式的美化。上传完成生成的文件名将会显示在.upload-file-result里面,.progress是进度条的位置,先让他隐藏加上 hidden的class,.progress-bar是进度条的主体,.progress-bar-status是进度条的文本提醒。
去掉hidden的class,看到的效果是这样的
[图片上传失败...(image-2c700a-1548557865446)]
将上传事件绑定在file的input里面,绑定方式就随意了。
var progress=$(".progress-bar"), status=$(".progress-bar-status"), percentVal='0%';//上传步骤$("#myupload").ajaxSubmit({ url: uploadUrl, type:"POST", dataType:'json', beforeSend: function(){$(".progress").removeClass("hidden"); progress.width(percentVal); status.html(percentVal);}, uploadProgress: function(event, position, total, percentComplete){ percentVal= percentComplete+'%'; progress.width(percentVal); status.html(percentVal); console.log(percentVal, position, total);}, success: function(result){ percentVal='100%'; progress.width(percentVal); status.html(percentVal);//获取上传文件信息 uploadFileResult.push(result);// console.log(uploadFileResult);$(".upload-file-result").html(result.name);$("#upload-input-file").val('');}, error: function(XMLHttpRequest, textStatus, errorThrown){ console.log(errorThrown);$(".upload-file-result").empty();}});
[图片上传失败...(image-3d6ae0-1548557865446)]
[图片上传失败...(image-9f0adf-1548557865446)]
更多用法可以参考官网
文章到此结束,如果本次分享的jquery进度条和使用jquery.form.js实现文件上传及进度条前端代码的问题解决了您的问题,那么我们由衷的感到高兴!