【问题标题】:How to show buttons after uploading picture in html?在html中上传图片后如何显示按钮?
【发布时间】:2017-05-04 13:38:44
【问题描述】:

我创建了一个包含图像和相关控件(如编辑、删除和添加按钮)的 html 页面。目的是在默认情况下您只需要显示加号图标,上传图像后隐藏加号按钮并显示编辑和删除按钮。但是现在的问题是,当悬停图像时,所有控件都同时显示,并且很难用鼠标单击删除或编辑按钮。如何在html中上传个人资料图片后显示编辑和删除按钮,以及当有默认图片或同一div中没有​​图片时如何显示加号图标。 谁能帮帮我?

[默认头像][1]

[上传头像后][2]

请看附图。可以用jquery来完成吗?

.profile_image {position: relative; padding: 0px 40px; margin-bottom: 40px;}
    .add_new_candidate {margin: 20px 10px;}
    .profile_img_ctrls {position: absolute; width: 100%; height: 100%; display: none;}
    .profile_img_ctrls a {background: #8aa2ad; border-radius: 50%; width: 20px;  height: 20px; float: left; text-align: center; color: #fff; margin-right: 15px;}
    .profile_add_picture {width: 30px !important; height: 30px !important; margin-top: -8px; position: relative; overflow: hidden;}
    .add_new_candidate input {margin-top: 45px; font-size: 20px; font-weight: bold;}
    .profile_add_picture input.upload {position: absolute; top: 0; right: 0; margin: 0; padding: 0; font-size: 20px; cursor: pointer; opacity: 0; filter: alpha(opacity=0);}
    .profile_add_picture i {font-size: 20px; padding-top: 5px;}
<div class="row">
                        <div class="add_new_candidate profile_image">
                        	<div class="col-md-3">
                            	<img class="img-responsive img-circle" src="https://cdn2.iconfinder.com/data/icons/ios-7-icons/50/user_male2-512.png" >
                                <div class="profile_img_ctrls">
                                	<a href="#"><i class="fa fa-pencil"></i></a>
                                    <a class="profile_add_picture" href="#">
                                    	<i class="fa fa-plus"></i>
                                    	<input type="file" class="upload" />
                                    </a>
                                    <a href="#"><i class="fa fa-trash"></i></a>
                                </div>
                            </div>
                            
                            <div class="col-md-9">
                            	<input type="text" class="form-control" placeholder="Full name" >
                            </div>
                        </div>
                    </div>

【问题讨论】:

  • 抱歉,我没听懂这个问题。也许如果你写得更清楚......
  • 在你的解释中包含一张图片会很有帮助。
  • 如果你能生成一个可运行的代码 sn-p 这将比图像更有帮助 - 我们中的一些人在公司代理背后,无法在 SO 上查看图像。
  • 你使用任何框架吗?例如Vue、Angular、React?
  • 图片是如何添加的?有CMS吗? AJAX 调用?一些前端JS框架?通常,您会参与该过程。但如果没有更多信息,就无法回答这个问题。

标签: javascript html css twitter-bootstrap-3


【解决方案1】:

只需使用 css 选择器即可。基本上将defaut-image类添加到默认图像,并在用户上传图像时将此类更改为user-image并使用下面的代码

1.当有默认图片时

.profile_img_ctrls a{
    display: none
}
.default-image + .profile_img_ctrls .profile_add_picture {
   display: inline-block;
}
<div class="row">
    <div class="add_new_candidate profile_image">
        <div class="col-md-3">
           <img class="img-responsive img-circle defaut-image" src="dist/img/avatar.png" >
           <div class="profile_img_ctrls">
               <a href="#"><i class="fa fa-pencil"></i></a>
               <a class="profile_add_picture" href="#">
                   <i class="fa fa-plus"></i>
                   <input type="file" class="upload" />
               </a>
               <a href="#"><i class="fa fa-trash"></i></a>
           </div>
      </div>
            
      <div class="col-md-9">
          <input type="text" class="form-control" placeholder="Full name" >
      </div>
   </div>
   </div>
</div>

2。当用户更改图片时

.user-image + .profile_img_ctrls a {
    display: inline-block;
}
<div class="row">
   <div class="add_new_candidate profile_image">
       <div class="col-md-3">
           <img class="img-responsive img-circle user-image" src="dist/img/avatar.png" >
           <div class="profile_img_ctrls">
               <a href="#"><i class="fa fa-pencil"></i></a>
               <a class="profile_add_picture" href="#">
                   <i class="fa fa-plus"></i>
                   <input type="file" class="upload" />
               </a>
               <a href="#"><i class="fa fa-trash"></i></a>
           </div>
       </div>

       <div class="col-md-9">
           <input type="text" class="form-control" placeholder="Full name" >
       </div>
   </div>
  </div>
</div>

【讨论】:

  • 感谢 Rakesh Soni 的回复。可以使用jquery完成吗?我已经修改了问题并附上了一些截图。请检查。
【解决方案2】:

使用 Javascript 检查图像。 如果是默认图片显示上传按钮,否则显示编辑,删除按钮

我的示例代码

$(document).ready(function(){
	showButton(); // check image for show button

	function showButton() {
		var default_image_name = 'anonymous_avatar.png'; // default image name
		var image_now = $('#image-show img').attr('src');
		//default name indexof != -1
		if(image_now.indexOf(default_image_name) != -1 ){
			DefaultImageButton();
		} else {
			ChangeImageButton();
		}
	}

	function DefaultImageButton(){
		$('#BtnUpload').show();
		$('#BtnEdit').hide();
		$('#BtnDel').hide();
	}
	function ChangeImageButton(){
		$('#BtnUpload').hide();
		$('#BtnEdit').show();
		$('#BtnDel').show();
	}
});
#BtnEdit, #BtnDel{
	  width: 30px;
	  height: 30px;
	  color: white;
	}
	#BtnEdit{
	  background-color:blue;
	}
	#BtnDel{
	  background-color:red;
	}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

<div id="avatar-frame">

  <div id="image-show"><img src="https://jnswire.s3.amazonaws.com/jns-media/ea/05/91552/anonymous_avatar.png"></div>
	<input id="BtnUpload" type="file" accept ="image/*">
	<div id="BtnEdit">Edit</div>
	<div id="BtnDel">Del</div>

</div>

如果你对图片做一些事情(上传完成,删除......)你只需要打电话showButton()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 2020-09-27
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多