隐藏图像后面的输入字段文件

Posted

技术标签:

【中文标题】隐藏图像后面的输入字段文件【英文标题】:Hide input field file behind image 【发布时间】:2019-10-22 01:14:50 【问题描述】:

我正在尝试在头像后面添加一个file input 字段。单击头像时,用户应该能够选择图像。它工作得很好,但是我如何隐藏输入字段(请参阅 sn-p)。当我添加visible: hidden 时,它会消失,但显然它不再是可点击的了。

我该如何解决这个问题?我正在使用TailwindCss:

<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet"/>


<div class="flex items-center cursor-pointer justify-center relative w-16 h-16 rounded-full border-2 border-brand-100">
    <div>
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 32 32" style="enable-background:new 0 0 32 32;" xml:space="preserve"  ><g><g><g><path d="M28,6h-4l-4-4h-8.001L8,6H4c0,0-4,0-4,4v12c0,4,4,4,4,4s5.662,0,11.518,0    c1.614,2.411,4.361,3.999,7.482,4c3.875-0.002,7.167-2.454,8.436-5.889C31.995,23.076,32,22,32,22s0-8,0-12S28,6,28,6z     M14.033,21.66C11.686,20.848,10,18.626,10,16c0-3.312,2.684-6,6-6c1.914,0,3.607,0.908,4.706,2.306    C16.848,13.321,14,16.822,14,21C14,21.223,14.018,21.441,14.033,21.66z M23,27.883c-3.801-0.009-6.876-3.084-6.885-6.883    c0.009-3.801,3.084-6.876,6.885-6.885c3.799,0.009,6.874,3.084,6.883,6.885C29.874,24.799,26.799,27.874,23,27.883z" data-original="#010002" class="active-path" data-old_color="##565A5" fill="#565A5C"/><polygon points="24.002,16 22,16 22,20 18,20 18,22 22,22 22,26 24.002,26 24.002,22 28,22 28,20     24.002,20   " data-original="#010002" class="active-path" data-old_color="##565A5" fill="#565A5C"/></g></g></g> </svg>
    </div>

    <input id="file"
           class="absolute w-full h-full"
           ref="file"
           type="file"
           accept="image/*"/>
</div>

谢谢!

【问题讨论】:

【参考方案1】:

使用标签标签:https://www.w3schools.com/tags/tag_label.asp;

<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet"/>


<div class="flex items-center cursor-pointer justify-center relative w-16 h-16 rounded-full border-2 border-brand-100">
<label for="file">
    <div>
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 32 32" style="enable-background:new 0 0 32 32;" xml:space="preserve"  ><g><g><g><path d="M28,6h-4l-4-4h-8.001L8,6H4c0,0-4,0-4,4v12c0,4,4,4,4,4s5.662,0,11.518,0    c1.614,2.411,4.361,3.999,7.482,4c3.875-0.002,7.167-2.454,8.436-5.889C31.995,23.076,32,22,32,22s0-8,0-12S28,6,28,6z     M14.033,21.66C11.686,20.848,10,18.626,10,16c0-3.312,2.684-6,6-6c1.914,0,3.607,0.908,4.706,2.306    C16.848,13.321,14,16.822,14,21C14,21.223,14.018,21.441,14.033,21.66z M23,27.883c-3.801-0.009-6.876-3.084-6.885-6.883    c0.009-3.801,3.084-6.876,6.885-6.885c3.799,0.009,6.874,3.084,6.883,6.885C29.874,24.799,26.799,27.874,23,27.883z" data-original="#010002" class="active-path" data-old_color="##565A5" fill="#565A5C"/><polygon points="24.002,16 22,16 22,20 18,20 18,22 22,22 22,26 24.002,26 24.002,22 28,22 28,20     24.002,20   " data-original="#010002" class="active-path" data-old_color="##565A5" fill="#565A5C"/></g></g></g> </svg>
    </div>
</label>

    <input id="file"
           class="absolute w-full h-full"
           ref="file"
           type="file"
           accept="image/*" style=" visibility: hidden;"/>
</div>

【讨论】:

【参考方案2】:

你可以那样做,这不是官方的做法,但在这种情况下你可以使用这种方法

input[type='file'] 
  opacity: 0;
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet"/>


<div class="flex items-center cursor-pointer justify-center relative w-16 h-16 rounded-full border-2 border-brand-100">
    <div>
        <svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 32 32" style="enable-background:new 0 0 32 32;" xml:space="preserve"  ><g><g><g><path d="M28,6h-4l-4-4h-8.001L8,6H4c0,0-4,0-4,4v12c0,4,4,4,4,4s5.662,0,11.518,0    c1.614,2.411,4.361,3.999,7.482,4c3.875-0.002,7.167-2.454,8.436-5.889C31.995,23.076,32,22,32,22s0-8,0-12S28,6,28,6z     M14.033,21.66C11.686,20.848,10,18.626,10,16c0-3.312,2.684-6,6-6c1.914,0,3.607,0.908,4.706,2.306    C16.848,13.321,14,16.822,14,21C14,21.223,14.018,21.441,14.033,21.66z M23,27.883c-3.801-0.009-6.876-3.084-6.885-6.883    c0.009-3.801,3.084-6.876,6.885-6.885c3.799,0.009,6.874,3.084,6.883,6.885C29.874,24.799,26.799,27.874,23,27.883z" data-original="#010002" class="active-path" data-old_color="##565A5" fill="#565A5C"/><polygon points="24.002,16 22,16 22,20 18,20 18,22 22,22 22,26 24.002,26 24.002,22 28,22 28,20     24.002,20   " data-original="#010002" class="active-path" data-old_color="##565A5" fill="#565A5C"/></g></g></g> </svg>
    </div>

    <input id="file"
           class="absolute w-full h-full"
           ref="file"
           type="file"
           accept="image/*"/>
</div>

【讨论】:

以上是关于隐藏图像后面的输入字段文件的主要内容,如果未能解决你的问题,请参考以下文章

Worklight/Dojo - 键盘隐藏输入字段

PHP:通过图像提交按钮传递选择和隐藏输入的值

iOS - 隐藏在键盘后面的 UIView 容器

如何隐藏文本框边框后面的东西?

软输入键盘隐藏编辑文本

为啥拍照后图像按钮隐藏在表面视图后面? [关闭]