【问题标题】:Custom placeholder in input with icon输入中带有图标的自定义占位符
【发布时间】:2015-09-02 19:47:59
【问题描述】:

我正在尝试为我的“搜索”输入创建一个自定义占位符。它应该看起来像一个搜索图标(为此使用 Bootstrap glyphicon glyphicon-search 类),然后是输入元素内的“搜索”一词,就像占位符一样,并且居中。

我正在尝试将包含这些的 div 定位到输入中的元素,但我无法正确处理。

这是jsfiddle中的代码。

HTML:

<div class="search-wrapper">
    <form class="post_search" id="post_search" action="/posts/explore" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
        <input autocomplete="off" class="search-input" type="search" name="q[caption_or_user_user_name_cont]" id="q_caption_or_user_user_name_cont">
        <div class="placeholder">
            <div>
                <span class="glyphicon glyphicon-search"></span>
                <span>Search</span>
            </div>
        </div>
    </form>
</div>

CSS:

.search-wrapper {
  max-width: 340px;
  margin: 0 auto;
  text-align: center;
  margin-top: 20px;
  display: inline;
}

.search-wrapper .search-input {
  border: 1px solid #ddd;
  border-radius: 15px;
  background-color: #eee;
  width: 220px;
  height: 31px;
  padding: 10px 15px;
  transition: 0.25s all;
}

.search-wrapper .search-input:focus {
  outline: 0 none;
  background-color: #fff;
  transition: 0.25s all;
}

.search-wrapper .placeholder {
  display: inline;
  position: relative;
  top: 30%;
  width: 40px;
  margin: 0 auto;
  text-align: center;
  font-size: 10px;
}

那么当专注于输入时,占位符应该消失了,我想这对一些js来说应该不难。

但回到问题,我做错了什么?如何按预期显示占位符?

【问题讨论】:

  • 我已经想提一下,由于您的元素是如何定位的,您甚至不需要 javascript,只需使用 input:focus + .placeholder 定位即可
  • 抱歉,我对 sn-ps 不熟悉...您在 jsfiddle 中有代码,这与我的工作示例非常接近。你是对的,我可以这样定位它并添加display: none 属性。无论如何,这不是我要问的。
  • 想试试这样的:jsfiddle.net/83x8tfwp/3

标签: css


【解决方案1】:

为什么不大大地简化呢?您已经拥有 HTML 中内置的placeholder!您可以执行以下操作:

input[type="search"]::-webkit-input-placeholder:before {
  content: "\e003  ";
  font-family: 'Glyphicons Halflings';
}

input[type="search"]:-moz-placeholder:before {
  content: "\e003  ";
  font-family: 'Glyphicons Halflings';

input[type="search"]::-moz-placeholder:before {
  content: "\e003  ";
  font-family: 'Glyphicons Halflings';
}

input[type="search"]:-ms-input-placeholder:before {  
  content: "\e003  ";
  font-family: 'Glyphicons Halflings';
}
&lt;input type='search' placeholder='search here' /&gt;

现在不用担心,图标没有显示在这里,因为我没有包含 font-awesome(或任何 Glyphicons Halflings 提供的字体),但这使得它非常简单地创建一个漂亮的占位符。它甚至可以像一个一样工作!它还大大减少了您的代码,尽管浏览器支持不那么出色(这实际上取决于您想要回溯多远)。

Font Awesome 使用 unicode 字形和包含所有这些图标的字体,因此只要您使用正确的字体并将正确的字符复制到您的 content 属性中,这将起作用。

这是在 Safari 和 Chrome 中测试的

【讨论】:

  • 如果我这样做,我如何在输入焦点时隐藏占位符?
  • 它会自动执行此操作!
  • 大声笑是的,我想我宁愿这样做,因为使用 div 如果您单击它,您不会专注于输入。有什么方法可以让占位符居中?
  • 不确定。 text-align: center 工作吗..?我想提一下,并非所有浏览器都支持这一点。我无法让它在 FF tbh 上运行......
【解决方案2】:

我的解决方案:

https://jsfiddle.net/83x8tfwp/6/

HTML:

<div class="search-wrapper">
    <form class="post_search" id="post_search" action="/posts/explore" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
        <input autocomplete="off" class="search-input" type="search" name="q[caption_or_user_user_name_cont]" id="q_caption_or_user_user_name_cont">
        <div class="placeholder">
            <div>
                <span class="glyphicon glyphicon-search"></span>
                <span>Search</span>
            </div>
        </div>
    </form>
</div>

CSS:

.search-wrapper {
  max-width: 280px;
  margin: 0 auto;
  text-align: center;
  margin-top: 20px;
  position: relative;
  color: #aaa;
}

.search-wrapper .search-input {
  border: 1px solid #ddd;
  border-radius: 15px;
  background-color: #eee;
  width: 220px;
  height: 31px;
  padding: 3px 15px;
  transition: 0.25s all;
}

.search-wrapper .search-input:focus {
  outline: 0 none;
  background-color: #fff;
  transition: 0.25s all;
}

.search-wrapper .search-input:focus + .placeholder {
  display: none;
}

.search-wrapper .placeholder {
  position: absolute;
  top: 8px;
  left: 38%;
  margin: 0 auto;
  text-align: center;
  font-size: 13px;
}

JavaScript:

$('.placeholder').on('click', function() {
    $('.search-input').focus();
});

if ($('.search-input').val()) {
    $('.placeholder').hide();
}

$('.search-input').on('blur', function() {
    if (!$('.search-input').val()) {
        $('.placeholder').show();
    }
});

$('.search-input').on('focus', function() {
    if (!$('.search-input').val()) {
        $('.placeholder').hide();
    }
});

$('.search-input').on('input', function() {
    if ($('.search-input').val()) {
        $('.placeholder').hide();
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2021-03-13
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 2013-12-12
    相关资源
    最近更新 更多