我正在尝试弄清楚如何围绕矩形实现进度条。让我们想象一下,我有一个 5 像素(黑色)边框的 div 500x300。
我希望进度条从左上角开始,然后转到 -> 右上角 -> 右下角 -> 左下角 -> 然后回到起点。
最佳答案
纯 CSS:
这种效果可以通过 CSS 使用多个线性渐变作为背景并适当定位它们来实现。方法如下:
linear-gradient 背景。边框的粗细决定了 background-size。也就是说,如果边框厚度为 5px,则产生顶部和底部边框的线性渐变将为 100% 5px(100% 宽度 5px 高度),而产生左右边框的线性渐变将为 5px 100%(3px 宽 100% 高)。background-position 设置为不显示任何边框。在动画期间,我们将每个背景渐变动画到其正确位置。这会产生具有动画边框的效果。我在下面的代码片段中使用了 CSS 关键帧,因此它会自动从头到尾设置动画(也就是说,它仅在绘制完整边框后停止)但是如果您希望对其进行更多控制(并说中途停止如在进度条中)那么你可以使用 JS 并根据进度百分比修改 background-position。
.progress {
height: 300px;
width: 500px;
background: linear-gradient(to right, black 99.99%, transparent), linear-gradient(to bottom, black 99.99%, transparent), linear-gradient(to right, black 99.99%, transparent), linear-gradient(to bottom, black 99.99%, transparent);
background-size: 100% 5px, 5px 100%, 100% 5px, 5px 100%;
background-repeat: no-repeat;
animation: progress 4s linear forwards;
background-position: -500px 0px, 495px -300px, 500px 295px, 0px 300px;
}
@keyframes progress {
0% {
background-position: -500px 0px, 495px -300px, 500px 295px, 0px 300px;
}
25% {
background-position: 0px 0px, 495px -300px, 500px 295px, 0px 300px;
}
50% {
background-position: 0px 0px, 495px 0px, 500px 295px, 0px 300px;
}
75% {
background-position: 0px 0px, 495px 0px, 0px 295px, 0px 300px;
}
100% {
background-position: 0px 0px, 495px 0px, 0px 295px, 0px 0px;
}
}<!-- prefix free library is only to avoid browser prefixes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class="progress"></div>
没有自动动画的 CSS 版本:
这是该片段的 CSS 版本,它接受输入百分比值并根据该值设置边框。在文本框中输入 0 到 100 之间的值,然后单击 Enter。
window.onload = function() {
var progress = document.querySelector('.progress'),
totalLength = (progress.offsetWidth * 2) + (progress.offsetHeight * 2);
var btn = document.querySelector('#enter'),
progressVal = document.querySelector('#progress');
btn.addEventListener('click', function() {
input = (progressVal.value > 100) ? 100 : progressVal.value;
borderLen = (input / 100) * totalLength;
console.log(borderLen);
if (borderLen <= progress.offsetWidth) {
backgroundPos = 'background-position: ' + (-500 + borderLen) + 'px 0px, 495px -300px, 500px 295px, 0px 300px';
progress.setAttribute('style', backgroundPos);
} else if (borderLen <= (progress.offsetWidth + progress.offsetHeight)) {
backgroundPos = 'background-position: 0px 0px, 495px ' + (-300 + (borderLen - progress.offsetWidth)) + 'px, 500px 295px, 0px 300px';
progress.setAttribute('style', backgroundPos);
} else if (borderLen <= (progress.offsetWidth * 2 + progress.offsetHeight)) {
backgroundPos = 'background-position: 0px 0px, 495px 0px, ' + (500 - (borderLen - progress.offsetWidth - progress.offsetHeight)) + 'px 295px, 0px 300px';
progress.setAttribute('style', backgroundPos);
} else {
backgroundPos = 'background-position: 0px 0px, 495px 0px, 0px 295px, 0px ' + (300 - (borderLen - (progress.offsetWidth * 2) - progress.offsetHeight)) + 'px';
progress.setAttribute('style', backgroundPos);
}
});
};.progress {
height: 300px;
width: 500px;
margin-top: 20px;
background: linear-gradient(to right, black 99.99%, transparent), linear-gradient(to bottom, black 99.99%, transparent), linear-gradient(to right, black 99.99%, transparent), linear-gradient(to bottom, black 99.99%, transparent);
background-size: 100% 5px, 5px 100%, 100% 5px, 5px 100%;
background-repeat: no-repeat;
background-position: -500px 0px, 495px -300px, 500px 295px, 0px 300px;
}<!-- prefix free library is only to avoid browser prefixes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<input id='progress' type='text' />
<button id='enter'>Set Progress</button>
<div class="progress"></div>
使用 SVG:
对于 SVG,方法如下:
path 元素,使其形成框的边框,并使用 getTotalLength() 方法获取其长度。stroke-dasharray和 stroke-dashoffset path 的属性,这样路径最初是不可见的。stroke-dashoffset,我们可以产生类似进度条的效果。我再次使用动画自动触发从开始到结束的移动,但如果你想要一个类似进度条的效果,你可以删除动画并根据进度百分比设置偏移量。
window.onload = function() {
var progress = document.querySelector('.progress path');
var borderLen = progress.getTotalLength() + 5,
offset = borderLen;
progress.style.strokeDashoffset = borderLen;
progress.style.strokeDasharray = borderLen + ',' + borderLen;
anim = window.requestAnimationFrame(progressBar);
function progressBar() {
offset -= 1;
progress.style.strokeDashoffset = offset;
anim = window.requestAnimationFrame(progressBar);
if (offset < 0)
window.cancelAnimationFrame(anim);
}
};.progress {
height: 300px;
width: 500px;
}
.progress svg {
height: 100%;
width: 100%;
}
path {
stroke: black;
stroke-width: 5;
fill: none;
}<div class="progress">
<svg viewBox='0 0 510 310' preserveAspectRatio='none'>
<path d='M5,5 505,5 505,305 5,305 5,2.5' />
<!-- end is start point - stroke width/2 -->
</svg>
</div>
没有自动动画的 SVG 版本:
这是该代码段的 SVG 版本,它接受输入百分比值并根据该值设置边框。在文本框中输入 0 到 100 之间的值,然后单击 Enter。
window.onload = function() {
var progress = document.querySelector('.progress path');
var borderLen = progress.getTotalLength() + 5,
offset;
progress.style.strokeDashoffset = borderLen;
progress.style.strokeDasharray = borderLen + ',' + borderLen;
var btn = document.querySelector('#enter'),
progressVal = document.querySelector('#progress');
btn.addEventListener('click', function(){
input = (progressVal.value > 100) ? 100 : progressVal.value;
offsetToSet = (input/100) * borderLen;
console.log(borderLen - offsetToSet);
progress.style.strokeDashoffset = borderLen - offsetToSet;
});
};.progress {
height: 300px;
width: 500px;
}
.progress svg {
height: 100%;
width: 100%;
}
path {
stroke: black;
stroke-width: 5;
fill: none;
}<input id='progress' type='text'/>
<button id='enter'>Set Progress</button>
<div class="progress">
<svg viewBox='0 0 510 310' preserveAspectRatio='none'>
<path d='M5,5 505,5 505,305 5,305 5,2.5' />
<!-- end is start point - stroke width/2 -->
</svg>
</div>
关于javascript - 沿着矩形边界的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31996110/
我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r
我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"和data-method="delete"的链接。当我单击该链接时,我可以看到对我的Rails服务器的DELETE请求。返回的JS代码会更改此链接的属性,其中包括href和data-method。再次单击此链接后,我的服务器收到了对新href的请求,但使用的是旧的data-method,即使我已将其从DELETE到POST(它仍然发送一个DELETE请求)。但是,如果我刷新页面,HTML与"new"HTML相同(随返回的JS发生变化),但它实际上发送了正确的请求类型。这就是这个问题令我困惑的
我有这个:AccountSummary我想单击该链接,但在使用link_to时出现错误。我试过:bot.click(page.link_with(:href=>/menu_home/))bot.click(page.link_with(:class=>'top_level_active'))bot.click(page.link_with(:href=>/AccountSummary/))我得到的错误是:NoMethodError:nil:NilClass的未定义方法“[]” 最佳答案 那是一个javascript链接。Mechan
我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文
我有一个用Rails3编写的站点。我的帖子模型有一个名为“内容”的文本列。在帖子面板中,html表单使用tinymce将“content”列设置为textarea字段。在首页,因为使用了tinymce,post.html.erb的代码需要用这样的原始方法来实现。.好的,现在如果我关闭浏览器javascript,这个文本区域可以在没有tinymce的情况下输入,也许用户会输入任何xss,比如alert('xss');.我的前台会显示那个警告框。我尝试sanitize(@post.content)在posts_controller中,但sanitize方法将相互过滤tinymce样式。例如
出于某种原因,我必须为Firefox禁用javascript(手动,我们按照提到的步骤执行http://support.mozilla.org/en-US/kb/javascript-settings-for-interactive-web-pages#w_enabling-and-disabling-javascript)。使用Ruby的SeleniumWebDriver如何实现这一点? 最佳答案 是的,这是可能的。而是另一种方式。您首先需要查看链接Selenium::WebDriver::Firefox::Profile#[]=
我是Ruby和Watir-Webdriver的新手。我有一套用VBScript编写的站点自动化程序,我想将其转换为Ruby/Watir,因为我现在必须支持Firefox。我发现我真的很喜欢Ruby,而且我正在研究Watir,但我已经花了一周时间试图让Webdriver显示我的登录屏幕。该站点以带有“我同意”区域的“警告屏幕”开头。用户点击我同意并显示登录屏幕。我需要单击该区域以显示登录屏幕(这是同一页面,实际上是一个表单,只是隐藏了)。我整天都在用VBScript这样做:objExplorer.Document.GetElementsByTagName("area")(0).click
🎉精彩专栏推荐💭文末获取联系✍️作者简介:一个热爱把逻辑思维转变为代码的技术博主💂作者主页:【主页——🚀获取更多优质源码】🎓web前端期末大作业:【📚毕设项目精品实战案例(1000套)】🧡程序员有趣的告白方式:【💌HTML七夕情人节表白网页制作(110套)】🌎超炫酷的Echarts大屏可视化源码:【🔰Echarts大屏展示大数据平台可视化(150套)】🔖HTML+CSS+JS实例代码:【🗂️5000套HTML+CSS+JS实例代码(炫酷代码)继续更新中…】🎁免费且实用的WEB前端学习指南:【📂web前端零基础到高级学习视频教程120G干货分享】🥇关于作者:💬历任研发工程师,技术组长,教学总监;
我有一个Rails应用程序。还有一个javascript(javascript1.js)文件必须包含在每个View的最底部。我把它放在/assets/javascripts文件夹中。Application.js包含以下代码//=requirejquery//=requirejquery_ujs//=someotherfiles//=require_directory.即使Application.js中不包含javascript1.js,它也会自动包含,不是吗?那么我怎样才能做我想做的事呢? 最佳答案 单独定义、包含和执行您的java
如何生成指向javascript文件的绝对链接。我想应该有类似下面的东西(不幸的是它似乎不可用):javascript_url'main'#->'http://localhost:3000/javascripts/main.js'代替:javascript_path'main'#->'/javascripts/main.js'我需要绝对URL,因为该javascript文件将用于书签。另外我需要相同的css文件。谢谢,德米特里。 最佳答案 javascript和css文件的绝对URL现在在Rails4中可用ActionView::H