简体   繁体   English

选择下拉菜单并设置其样式

[英]Select drop-down and styling it's options

I want my select box and the options drop down to be styled as shown in the image below. 我希望我的选择框和选项下拉菜单的样式如下图所示。 Please help me with the same. 请同样帮我。 If only CSS can do that it would be great, else a javascript solution. 如果只有CSS可以做到,那就太好了,否则,可以使用JavaScript解决方案。 Thanks in advance. 提前致谢。

当前选择下拉菜单

新要求

I am very new to Javascript and the front-end technologies. 我对Javascript和前端技术非常陌生。 Currently, the below code is being used for the select box and getting it's options. 当前,以下代码用于选择框并获取其选项。

                        <div class="selectWrapper">
                                <label for="ext-comp-1001" class="inputLabel"><span class="required">*</span><fmt:message key="registration.country"/></label> 
                                <div class="x-form-element" id="x-form-el-ext-comp-1001">
                                    <input id="brand" name="spEntityID" type="hidden" value="<%=spEntityID%>" />
                                    <select autocomplete="off" id="ext-comp-1001" name="country" value="" class=" x-form-select-one x-form-field select_one select_one" onchange="changeStateList()"></select>
                                </div>
                            </div>

In the above code some Javascript code brings all the countries and the Javascript code for it is all written in hexadecimal(UTF-8). 在上面的代码中,一些Javascript代码带到了所有国家/地区,并且所有Javascript代码都以十六进制(UTF-8)编写。 Can I achieve this without touching the existing Javascript code which is difficult to decrypt and implement all the functionalities. 我能否在不触及难以解密和实现所有功能的现有Javascript代码的情况下实现这一目标?

To achieve your task, you'll need some JavaScript along with a touch of CSS . 为了完成您的任务,您需要一些JavaScript以及一些CSS

I prepared a demo for you so you can build on it even though it does include all the functionalities you want (I mean you'll probably not going to add that much as I had it all done for you). 我为您准备了一个演示,因此即使它确实包含您想要的所有功能,您也可以在上面进行演示(我的意思是您可能不会像我为您所做的那样添加太多功能)。

So, how we'll we proceed : 因此,我们将如何进行:

  • Firstly, we'll prepare our HTML (especially the select element) as usual and we'll add another element that will host the new or the custom select which has : 首先,我们将照常准备HTML (尤其是select元素),并添加另一个元素来托管新的或custom select元素,该元素具有:

    • an element to show the selected option 's text. 显示所选option文本的元素。
    • an element that represents an arrow on the right (as that one which appears in the image included in the question). 一个代表右边箭头的元素(与出现在问题中的图像一样)。
    • an element to wrap the option s ( JavaScript will populate that element along with changing the selected element text accordingly). 一个用于包装option的元素( JavaScript会填充该元素,并相应地更改所选元素的文本)。
  • populate the custom select with based on the "real" select options. 根据“真实” select选项填充自定义select

  • change the custom select text with the selected option with JavaScript . 使用JavaScript使用selected option更改custom select文本。

The next example illustrates what's being said, also it has some helpful comments : 下一个示例说明了正在说的内容,还提供了一些有用的注释:

 /** select the elements that are going to build the main functionalities of this task **/ const select = document.getElementById("select-box"), realOptions = select.options, customSelect = document.getElementById("custom-select"), selectedText = customSelect.querySelector(".selected-option-text"), optionsContainer = customSelect.querySelector(".options-container"); let idx = 0; /** this will help us get the correct index of the option when assigning the click event listener to the newly added custom options **/ /** add click listener to the custom "select" to open the custom options container **/ customSelect.addEventListener("click", () => optionsContainer.classList.add("visible") ); /** looping through the "option" elements of the real "select" **/ [...realOptions].forEach(el => { let currIdx = idx++; /** that will help us change the selected "option" on the real "select" eleement **/ /** if the current real "option" is not disabled we add a custom one (in the custom "select") **/ if (el.disabled === false) { /** create a new "p" element that will act as an "option" **/ const customOption = document.createElement("p"); customOption.classList.add("custom-option"); /** the text of the "p" element is the same as the current (we're in a loop !) real "option" element **/ customOption.textContent = el.textContent; /** add click listener to the "p" element that handles the click on a custom option which is the "p" element **/ customOption.addEventListener("click", e => { /** the event doesn't propagate to the parent element to prevent the custom "select" from staying opened always (remember the click listener above for the parent (the custom "select")) **/ e.stopPropagation(); optionsContainer.classList.remove("visible"); realOptions.selectedIndex = currIdx; selectedText.textContent = el.textContent; }); /** add the "p" element which acts as an "option" to the custom "select" **/ optionsContainer.append(customOption); } }); 
 * { box-sizing: border-box; margin: 0; padding: 0; } /** for demo purposes **/ select#select-box { display: block; margin-bottom: 25px; } .custom-select { position: relative; max-width: 50%; /** changes this per your requirements it's not that necessary **/ padding-right: 10px; border: 2px solid #595859; border-radius: 6px; cursor: pointer; } .custom-select .arrows { position: absolute; top: 50%; right: 6px; transform: translate3d(0, -50%, 0); font-size: 0.75rem; } .custom-select .arrows>.fa { display: inline-block; vertical-align: middle; } .custom-select .selected-option-text { padding: 8px 6px; color: #595859; font-weight: bold; } .custom-select .options-container { display: none; position: absolute; width: 98%; max-height: 250px; top: 8px; left: 0; right: 0; margin: auto; overflow-x: hidden; overflow-y: auto; background-color: #595859; color: #f5f5f5; border-radius: 4px; } .custom-select .options-container.visible { display: block; } .custom-select .options-container>.custom-option { padding: 4px; cursor: default; transition: all .2s 0s ease; } .custom-select .options-container>.custom-option:hover { background-color: #ca2128; } 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.9.0/css/all.min.css" /> <select id="select-box"> <option value="" disabled selected>select option</option> <option value="1">option 1</option> <option value="2">option 2</option> <option value="3">option 3</option> <option value="4">option 4</option> <option value="5">option 5</option> <option value="6">option 6</option> <option value="7">option 7</option> <option value="8">option 8</option> <option value="9">option 9</option> <option value="10">option 10</option> </select> <!-- nothing too fancy here just the "#custom-select" elemnt that will contain the custom select --> <div id="custom-select" class="custom-select"> <div class="selected-option-text">Please select an option</div> <div class="arrows"> <i class="fa fa-sort"></i> </div> <div class="options-container"></div> </div> 

You can customize that example to more fulfill your wanted end result. 您可以自定义该示例,以更充分地实现所需的最终结果。

I also didn't hide the real select so you can see it changes based on the option you select on the custom one. 我也没有隐藏真正的select因此您可以看到它根据您在自定义选择中选择的选项而改变。

I'm here for any clarification you want. 我在这里是您想要的任何澄清。

Hope I pushed you further. 希望我能进一步推动您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM