限制div中的文本,但使整个div可单击

时间:2016-03-22 16:41:47

标签: javascript jquery html css

我根据我的要求制作了styled-select。现在的问题是,如果所选选项大于整个区域,那么它自然会填满整个div,如下图所示:

enter image description here

现在我希望它受限制或右边有一个边距,以便显示我用作background: url的箭头,如下图所示:

enter image description here

如果我减小div的宽度就可以了,但问题是它无法点击以打开下拉菜单。现在我想知道是否有限制文本的解决方案,但仍然有可点击区域?

这是我的代码: Working jsFiddle



$(function() {

   $('.styled-select select').hide();
   $("select#elem").val('0');

   $('.styled-select div').each(function() {
     var $container = $(this).closest('.styled-select');
     $(this).html($container.find('select option:selected').text());
   });

   $('.styled-select div').click(function() {
     var $container = $(this).closest('.styled-select');
     var opLen = $container.find('select').children('option').length;
     if (opLen < 5) {
       $container.find('select').show().attr('size', opLen).focus();
     } else {
       $container.find('select').show().attr('size', 5).focus();
     }
   });

   $('.styled-select select').click(function() {
     var $container = $(this).closest('.styled-select');
     var text = $container.find('select option:selected').text();
     $container.find('div').html(text);
     $container.find('select').hide();
   });

   $('.styled-select select').focusout(function() {
     var $container = $(this).closest('.styled-select');
     $container.find('select').hide();
   });

 });
&#13;
.styled-select select {
	  position: absolute;
	  background: transparent;
	  width: 420px;
	  padding-top: 5px;
	  font-size: 18px;
	  font-family: 'PT Sans', sans-serif;
	  color: black;
	  border: 0;
	  border-radius: 4;
	  -webkit-appearance: none;
	  -moz-appearance: none;
	  -o-appearance: none;
	  z-index: 1;
	  outline: none;
	  top: 42px;
	  box-shadow: 0 2px 2px 0 #C2C2C2;
	}
	
	.styled-select {
	  background: url('http://s22.postimg.org/f5gjjtswd/campaign_Selector.png') no-repeat right;
	  background-color: white;
	  width: 420px;
	  height: 42px;
	  position: relative;
	  margin: 0 auto;
	  box-shadow: 0 2px 2px 0 #C2C2C2;
	  background-position: 97% 50%;
	}
	
	.styled-select option {
	  font-size: 18px;
	  background-color: white;
	  margin-left: 3px;
	}
&#13;
<div class="styled-select" style="width: 301px; margin:5px 0 0 10px;">
  <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="https://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
  <div id="userChannelDivId" style="font-size:18px; position: absolute; top: 8px; left: 5px; width: 295px; height: 42px; white-space:nowrap; overflow:hidden"></div>
  <select id="userChannelId" name="userChannelId" style="width:100%">
    <option value="">resh resh reshadreshadreshadreshadreshad resh</option>
    <option value="">--- Select ---</option>
    <option value="">--- Celect ---</option>
    <option value="">--- Delect ---</option>
    <option value="">--- Felect ---</option>
    <option value="">--- Gelect ---</option>
  </select>
</div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:3)

更改#UserChannelDivID ...

width: 265px;
text-overflow: ellipsis; 
padding: 0 30px 0 0;

<强> Fiddle

答案 1 :(得分:1)

您可以调整div的大小,然后应用透明边框,将容器扩展到刻度线上。

因此:

#userChannelDivId {
  width: 250px !important;
  border-right: 45px solid transparent;
}

诀窍。

https://jsfiddle.net/qyf22h83/13/

答案 2 :(得分:0)

仅CSS解决方案

基于select-css Github repo,这是一个仅限CSS的解决方案。当HTML select标记提供所有功能时,无需花哨的JavaScript。此解决方案取代了下拉箭头。在custom-select div中换行选择并添加CSS。将下拉箭头定义替换为您自己的。

https://jsfiddle.net/sacwebdeveloper/ctchr3ph/

&#13;
&#13;
/* Container used for styling the custom select, the buttom class below adds the
 * bg gradient, corners, etc. */
.custom-select {
	position: relative;
	display: block;
}

/* This is the native select, we're making everything but the text invisible so
 * we can see the button styles in the wrapper */
.custom-select select {
	width: 100%;
	margin: 0;
	outline: none;
	padding: .6em .8em .5em .8em;
  
	/* Prefixed box-sizing rules necessary for older browsers */
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	box-sizing: border-box;

	/* Font size must be 16px to prevent iOS page zoom on focus */
	font-size: 16px;
}


/* Custom arrow sits on top of the select - could be an image, SVG, icon font,
 * etc. or the arrow could just baked into the bg image on the select. */
.custom-select::after {
	content: " ";
	position: absolute;
	top: 50%;
	right: 1em;
	z-index: 2;
	/* These hacks make the select behind the arrow clickable in some browsers */
	pointer-events: none;
	display: none;
}

.custom-select::after {
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 7px solid #666;
    margin-top: -3px;
}

@supports ( -webkit-appearance: none ) or ( appearance: none )
	/* Firefox <= 34 has a false positive on @supports( -moz-appearance: none )
	 * @supports ( mask-type: alpha ) is Firefox 35+
	 */
	or ( ( -moz-appearance: none ) and ( mask-type: alpha ) ) {
	
	/* Show custom arrow */
	.custom-select::after {
		display: block;
	}

	/* Remove select styling */
	.custom-select select {
		padding-right: 2em; /* Match-01 */
		/* inside @supports so that iOS <= 8 display the native arrow */
		background: none; /* Match-04 */
		/* inside @supports so that Android <= 4.3 display the native arrow */
		border: 1px solid transparent; /* Match-05 */

		-webkit-appearance: none;
		-moz-appearance: none;
		appearance: none;
	}
	.custom-select select:focus {
		border-color: #aaa; /* Match-03 */
	}
}

.custom-select {
  background-color: white;
  margin-top: 40px;
}
&#13;
<div class="custom-select">
  <select id="userChannelId" name="userChannelId">
    <option value="">resh resh reshadreshadreshadreshadreshad resh</option>
    <option value="">--- Select ---</option>
    <option value="">--- Celect ---</option>
    <option value="">--- Delect ---</option>
    <option value="">--- Felect ---</option>
    <option value="">--- Gelect ---</option>
  </select>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

这里是一个使用子字符串的javascript替代方法,它还附加一个省略号(3 ...)来表示此值比显示的值多。您可以将范围更改为您喜欢的范围,我只需将其设置为20个字符。

   $('.styled-select div').each(function() {
       var $container = $(this).closest('.styled-select');
       var value = $container.find('select option:selected').text();
       value = value.substring(0, 20) + '...';
       $(this).html(value);
   });