如何使用Bootstrap或CSS在按钮后的输入中设置文件上传设计

时间:2018-08-07 17:50:00

标签: html css twitter-bootstrap css3

我在这里实现文件上传的功能,但是当进入UI部分时,默认文件上传就像

<input type="file" name="data">

就像 默认文件上传:

enter image description here

我想要这样的事情

需要上传文件:

enter image description here

在这里,如果需要,我不想应用任何j查询或Java脚本,只是html,css,bootstrap吗?

2 个答案:

答案 0 :(得分:1)

您应该在引导程序4中使用文件浏览器/自定义表单。 该代码应如下所示:

<div class="custom-file">
  <input type="file" class="custom-file-input" id="customFile">
  <label class="custom-file-label" for="customFile">Choose file</label>
</div>

,该组件应如下所示:

希望它能起作用!

答案 1 :(得分:0)

使用此代码

// highlight drag area 
var fileinput = document.querySelector('.file-input');
var filedroparea = document.querySelector('.file-drop-area');
var jssetnumber = document.querySelector('.js-set-number');
fileinput.addEventListener('dragenter', isactive);
fileinput.addEventListener('focus', isactive);
fileinput.addEventListener('click', isactive);

// back to normal state
fileinput.addEventListener('dragleave', isactive);
fileinput.addEventListener('blur', isactive);
fileinput.addEventListener('drop', isactive);

// add Class
function isactive() {
  filedroparea.classList.add('is-active');
}

// change inner text
fileinput.addEventListener('change', function() {
  var count = fileinput.files.length;
  if (count === 1) {
    // if single file then show file name
    jssetnumber.innerText = fileinput.value.split('\\').pop();
  } else {
    // otherwise show number of files
    jssetnumber.innerText = count + ' files selected';
  }
});
@import url(https://fonts.googleapis.com/css?family=Lato:400,300,700);
body {
  background-color: #37385F;
  color: #D7D7EF;
  font-family: 'Lato', sans-serif;
}

h2 {
  text-align: center;
  margin: 50px 0;
}

.file-drop-area {
  border: 1px dashed #7c7db3;
  border-radius: 3px;
  position: relative;
  width: 450px;
  max-width: 100%;
  margin: 0 auto;
  padding: 26px 20px 30px;
  -webkit-transition: 0.2s;
  transition: 0.2s;
}
.file-drop-area.is-active {
  background-color: #3F4069;
}

.fake-btn {
  background-color: #3F4069;
  border: 1px solid #9E9EC4;
  border-radius: 3px;
  padding: 8px 15px;
  margin-right: 8px;
  font-size: 12px;
  text-transform: uppercase;
}

.file-msg {
  font-size: small;
  font-weight: 300;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-block;
  max-width: calc(100% - 130px);
  vertical-align: middle;
}

.file-input {
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  cursor: pointer;
  opacity: 0;
}
.file-input:focus {
  outline: none;
}
<h2>Styling Native File Input</h2> 

<div class="file-drop-area">
  <span class="fake-btn">Choose files</span>
  <span class="file-msg js-set-number">or drag and drop files here</span>
  <input class="file-input" type="file" multiple>
</div>