WordPress plugin for styling select. JQuery plugin for styling selectors. CSS classes that can be used to design a select

Golovna / Google Play
Soldatov Nikolay
Styling select Styling select using pure CSS without using third-party libraries or JavaScript code. And also a bonus: we’ll look at how to style the select option using additional JavaScript and jQuery.

The structure will be standard

Elephant Hippo Giraffe

So that the select displays row one using the size attribute with values ​​of 1.

Styling for our select

-webkit-appearance: none; -moz-appearance: none; -ms-appearance: none; appearance: none; background : url ("path/img.png" ) no-repeat right center ; outline: 0;

Ottrimomo

Elephant Hippo Giraffe

In the application, we have written several rows of ccs power appearance with vendor prefixes, so that power works the same in all browsers. Read below about power.

All the foldability was due to the replacement of the standard arrow with the rectangular one, instead of which the background was implemented. You can insert a picture this way. It is also necessary to adjust the size for the additional background-size box

Appearance CSS

css3 power appearance helped to implement our design

This power allows you to change the appearance of an element to: button, checkbox, radio, field, icon and much more. In our episode we cried out an element, vikoristuyuchi none and added a picture for additional background

Styling select option

To style the select option you need JavaScript

Vibrati HTML JavaScript PHP .select ( display : block ; max-width : 215px ; width : 100% ; position : relative ; ). relative ; ) .new-select__list ( border : 1px solid #ced4da ; cursor : pointer ; position : absolute ; top : 45px ; left : 0 ; width : 100% ; ) .new-select__list.on ( display : . new-select__item span ( display: block; padding: 10px 15px;) .new-select__item span:hover (color: #12b223;) .new-select :after (content: ""; : 25px; position: absolute; right: 9px; top: 9px ; -out , -o-transition : all .27s ease-in-out , transition : all .27s ease-in-out , -webkit-transform : rotate (0deg ); -ms-transform : rotate (0deg ); - o-transform : rotate (0deg); transform : rotate (0deg); ) .new-select.on :after ( -webkit-transform : rotate (180deg); -ms-transform : rotate (180deg); -o-transform : rotate (180deg ); transform : rotate (180deg ); )

There are jQuery connections in front of the JavaScript code

$(".select"). each (function () ( const _this = $ (this), selectOption = _this . find ( "option "), selectOptionLength = selectOption . length , selectedOption = selectOption . filter ( " :selected " ), duration = 45 animations _this . hide (); _this . wrap ( " " ); $ ( " " , ( class : " new-select " , text : _this . children ( " option:disabled " ). text () )). _this ); const selectHead = _this . next ( " . new-select " ); select__list " ); for (let i = 1; i< selectOptionLength ; i ++ ) { $ (" " , { class : " new-select__item " , html : $ (" " , { text : selectOption . eq (i ). text () }) }) . attr (" data-value ", selectOption . eq(i). val()). appendTo(selectList); ) const selectItem = selectList. find (" .new-select__item " ); selectList . slideUp (0 ); selectHead . on (" click " , function () { if ( ! $ (this ). hasClass (" on " ) ) { $ (this ). addClass (" on " ); selectList . slideDown (duration ); selectItem . on (" click " , function () { let chooseItem = $ (this ). data (" value " ); $ (" select " ). val (chooseItem ). attr (" selected " , " selected " ); selectHead . text ( $ (this ). find (" span " ). text () ); selectList . slideUp (duration ); selectHead . removeClass (" on " ); }); } else { $ (this ). removeClass (" on " ); selectList . slideUp (duration ); } }); });

Nowadays, designers have increasingly begun to draw small HTML forms in the style of HTML 2.0, this trend is becoming more and more important, and beautiful data entry fields and select fields with rounded edges and shadows become increasingly important. You can only please, and even everything is wonderful, and CSS3 is already being victorious in many ways, allowing you to give a great look to forms, fields and selectors. It’s a pity, don’t make everything so rosy.

Regardless of the fact that all modern browsers support HTML5 and CSS3, not everyone uses them, and according to statistics, half of the users of the entire Internet, as before, use browsers of older versions, and many of them do not Enjoy the full world or use CSS3. Therefore, styling the list (Select) that appears is often done in the good old way, using jQuery (js) and PNG or GIF images. I want to tell you about one of these methods.


Below is a script that I suddenly found on RuNet sites, unfortunately, I didn’t remember the address or the name of its author. I just convey great respect to you.

Review of the code JS

(function($)( $.fn.extend(( customStyle1: function(options) ( if(!$.browser.msie || ($.browser.msie&&$.browser.version>6))) ( return this. each (function() ( var currentSelected = $(this).find(":selected"); $(this).after(""+currentSelected.text()+")).css((position:"absolute" , opacity:0,fontSize:$(this).next().css("font-size")));var selectBoxSpan = $(this).next();var selectBoxWidth = parseInt($(this).width ( )) - parseInt(selectBoxSpan.css("padding-left")) -parseInt(selectBoxSpan.css("padding-right"));var selectBoxSpanInner = selectBoxSpan.find(":first-child"); selectBoxSpan.css ( (display:"inline-block")); selectBoxSpanInner.css((width:selectBoxWidth, display:"inline-block")); -top")) + parseInt(selectBoxSpan.css("padding-bottom")) ; $(this).height(selectBoxHeight).change(function())( selectBoxSpanInner.text($("option:selected",this).text()).parent().addClass("changed"); )) ; )); ) ) )); )) (jQuery);

Click on the script for styling "Selectu".

Review of the code JS

$(document).ready(function() ( $(".select1").customStyle1(); ));

HTML code "select", which will be stylized

Review of the code HTML

Option 1 Option 2 Option 3 Option 4 Option 5 Option 6

The style will be zastosovano until selectu

Review of the code CSS

.select1 ( width : 410px ; height : 32px ; color : #909090 ; font : 12px Arial, Tahoma, Helvetica, Verdana; text-align : left ; background : url (images/sel1.png) to design the select * / line- height : 30px ; white-space : nowrap ; / * protected by transfer * / padding : 0 22px 0 12px ; / * right-handed access with IE6 arrows * / margin : 3px 0 5px 0 ; )

By replacing the background image, you can style the select however you like and use this example in most browsers, like the oldest ones).

I often use a similar principle of stylization of form objects on great sites and vice versa, so that we can create a new idea, so that while most web masters are reluctant to stagnate such not very simple solutions, for the sake of promoting customer success with older browsers.

Good afternoon, habrausers and just read. Porivnyano recently asked how to set the styles to the select tag. We all want the stylization of the form to match the design of the site, but so far not everything can be described with pure CSS. In this article we will look at a simple example of writing your own select list, using CSS and a JavaScript language library - jQuery. I think that especially beginners will benefit from this material. Of course, it would be better to write in native JS, but everyone knows that there would be more rows of code, and it is unlikely that it would be more intelligent.

To be honest, I’ve tried to find a similar solution, but I didn’t know anything other than effective div wrappers for the select tag. And I thought that it would be bad to write something simple and necessary. Well, that's it!

Three files share the same fate with our plasticine molding:

  • selectbox.html
  • selectbox.css
  • selectbox.js
Let's look at them one by one. First of all, I really appreciate the simplest approach in this application - layout of the list or the selectbox.html file:

selectbox.html

Month

  • Sichen
  • Lutius
  • Berezen
  • Kviten
  • Traven
  • Cherven
  • Lipen
  • Serpen
  • Veresen
  • Zhovten
  • Leaf fall
  • chest


As can be seen from the output HTML code, our list will be presented to us in a month. Now let's look at the selectbox.css file:

selectbox.css

div#selectBox ( width: 250px; position: relative; height: 50px; border-radius: 3px; border: solid 1px lightgrey; background-color: #fff; color: #333; cursor: pointer; overflow: hidden; transition: .3s; ) div#selectBox p.valueTag ( padding: 15px; cursor: pointer; transition: .2s; height: 40px; ) div#selectBox > img.arrow ( position: absolute; right: 0; width: 50px; padding : 15px; ) /* for users of Safari, Chrome and Opera, a welcome bonus - a stylized scroll bar. */ ::-webkit-scrollbar ( background: transparent; width: 0.5em; position: absolute; ) ::-webkit-scrollbar-track ( background: transparent; position: absolute; z-index: -2; ) :: -webkit-scrollbar-thumb ( border-radius: 100px; background: #888; ) ul#selectMenuBox ( background: #fff; transition: .3s; width: 100%; height: 200px; overflow-y: auto; overflow- x: hidden !important; position: absolute; margin-top: 00px; display: block; ) ul#selectMenuBox > li ( display: block; li.option ( color: gray; padding: 10px; ) ul#selectMenuBox > li. option:hover ( color: #333; background: #e1e1e1; transition: .2s; )


There are no special difficulties here, since you deal with the basics of layout and layout using HTML and CSS3.

And now for the relish! Let's take a look at the plugin's output code selectbox() for jQuery, selectbox.js file:

selectbox.js

(function($) ( $.fn.selectbox = function() ( // cob parameters // set the standard height of the div "a. var selectDefaultHeight = $("#selectBox").height(); // rotate the image to div"e var rotateDefault = "rotate(0deg)"; // after pressing the button, a special function is used, which // calculates the output height of our div"a. // it’s easy to align with the input parameters (those that are specified in the script ) $("#selectBox > p.valueTag").click(function() ( // calculating the height of an object using the height() method var currentHeight = $("#selectBox").height(); // checking the mind escape/not escape from the given height of the head, // to understand what to do next if (currentHeight< 100 || currentHeight == selectDefaultHeight) { // если высота блока не менялась и равна высоте, заданной по умолчанию, // тогда мы открываем список и выбираем нужный элемент. $("#selectBox").height("250px"); // «точка остановки анимации» // здесь стилизуем нашу стрелку и делаем анимацию средствами CSS3 $("img.arrow").css({borderRadius: "1000px", transition: ".2s", transform: "rotate(180deg)"}); } // иначе если список развернут (высота больше или равна 250 пикселям), // то при нажатии на абзац с классом valueTag, сворачиваем наш список и // и присваиваем блоку первоначальную высоту + поворот стрелки в начальное положение if (currentHeight >= 250) ( $("#selectBox").height(selectDefaultHeight); $("img.arrow").css((transform: rotateDefault)); ) )); // this is how the list itself burns when you select the required element // and changes the text of the paragraph to the text of the element in the list $("li.option").click(function() ( $("#selectBox").height(selectDefaultHeight); $ ("img.arrow").css((transform: rotateDefault)); $("p.valueTag").text($(this).text()); )); ); )) (jQuery);


There was more code, but it was possible to squeeze in the css() and height() methods. Having issued a plugin for user-friendliness and a large-scale vikoristan. You can earn your money the way you should, or so I won’t be surprised that it optimizes my police force. To click, just connect the external file to the script and click on the plugin like this:

$("selector").selectbox();
First, enable the call in the ready() method of the document object, so that the plugin is loaded after the document is fully loaded. You can find out what this jQuery plugin is.

Anyone who is seriously involved in page layout knows that the possibilities of select styling in CSS are greatly enhanced. Even though a standard select often doesn’t fit into the design, you have to work with select styling via JavaScript. In fact, this article allows you to create your own select using JavaScript.

There are a lot of ready-made plugins, including jQuery, that allow you to do everything. If you don’t need a complex structure, it’s easier to build everything from scratch using pure JavaScript.

First of all, let's create the HTML structure of our new select :




Element 1
Element 2
Element 3



Element 1


Element 2


Element 3




As you know, there is almost no select tag here. Now we can easily style our select with additional CSS:

Select (
background-color: #0ee;
border-radius: 10px;
padding: 10px 0;
width: 200px;
}
.select p(
cursor: pointer;
margin: 0;
padding: 5px 20px;
}
.select p.active (
background-color: #ee0;
}

Insanely, you can earn anything you like. And, you decide, you need to change the look of the functionality of select , so that you select and highlight less than one element from the decals, as well as from managing the form at the same time. For whom are we speeding up JavaScript:

Function select(element) (
var value = element.getAttribute("data-value"); // Read the value of the selected element
var nodes = element.parentNode.childNodes; // Remove all other elements
for (var i = 0; i< nodes.length; i++) {
/* Third-party elements are filtered text and input */
if (nodes[i] instanceof HTMLParagraphElement) (
/* Adding active to the selected element, erasing this class from all others */
if (value == nodes[i].getAttribute("data-value")) nodes[i].className = "active";
else nodes[i].className = "";
}
}
document.getElementById("my_select").value = value; // Sets the hidden field's value
}

The key point in implementing the select functionality is the hidden field where the value from our select is recorded. The same information will be sent at the time of submitting the form.

The axis in such a simple way is absolutely timid, be it stylization of select JavaScript. If you use jQuery, the code will be even simpler.

Insanely, the standard select has more functionality. For example, it responds to tab, so you can also move through the elements using the arrow keys on the keyboard. But all this can also be implemented in JavaScript if necessary.

First recommendation. The code has a noscript tag, and it is not clear. Not everyone has JavaScript enabled, and in order for the form to work for such users, you need to enter a standard select. And everyone who has JavaScript enabled, please use our select tool.

Respect! Further development and support of the plugin in connection with what is now partly.

One of the most unacceptable (and I mean greedy) speeches in web development is the set of HTML forms. Unfortunately, there is no uniform standard for displaying form elements, regardless of the browser or operating system, as well as the ability to adequately design actions from these elements, vikoryst and cascading style sheets.

The following HTML form elements cannot be further stylized:

  • list of things to open;
  • ensign;
  • peremikach.
  • field for uploading the file.

As you can already see from the title of the post, there is more to say about selectivity.

There are a lot of ready-made solutions in the form of jQuery plugins for styling lists that open. However, I (through those that come from plugins, without taking into account these and other reasons) having chosen the way to the exit of my wheel and having written a power plugin, which I am sharing in this article.

I would like to point out that this plugin is not suitable for all possible situations of selection stagnation (read a few).

Demonstration of the robotic plugin

You can see the example of selector styling using an additional plugin. I created this design without using a vikoristan to depict it.

Advantages
  • When JavaScript is enabled, standard selectives appear.
  • The script is small in size, approximately 4 kilobytes.
  • Works in IE6+ and all current desktop browsers.
  • To be introduced internally.
  • Easily customizable via CSS.
  • Allows you to set the maximum height for the list (CSS power max-height).
  • Automatically adjusts the width if it is not specified.
  • Supports scrolling of the mouse wheel.
  • May have a “reasonable positioning”, that is. When you open the list, do not go beyond the visible part of the page.
  • You can “catch” the pressure of the Tab key and move the arrows on the keyboard.
  • Supports the "disabled" attribute.
  • Works with selectors that are dynamically added/changed.
Nedoliky
  • It does not support the multiple attribute, then. does not allow selecting multiple items (multi-select).
  • Does not support grouping of list elements (tag).
  • It is not advisable to move the arrows on the keyboard if the list of mouse clicks is open.
entice

The plugin is unavailable because VIN is not current.

jQuery plugin “SelectBox Styler”

Version: 1.0.1 | Zavantazhen: 11103 | Size: 7 KB | Last updated: 10/07/2012

Updated 09/22/2012 By reworking the script of the plugin (including by creating a minimized option), and also by adding support for the dynamic addition/change of selections. 10/07/2012 Corrected the behavior of the script when the onchange method of a tag is changed. Plugin connection

If jQuery is not yet enabled on the site, add a leading line before the tag:

Immediately after jQuery, connect the file with the script:

(function($) ( $(function() ( $("select").selectbox(); )) ))(jQuery)

This code should be placed before the tag after the specified files.

When dynamically changing selectors, it is necessary to launch the refresh trigger, for example:

(function($) ( $(function() ( $("button").click(function() ( $("select").find("option:nth-child(5))).attr("selected ", true); $("select").trigger("refresh"); )) )) ))(jQuery)

HTML code after the plugin is installed

Its structure looks like this:

-- Select --

  • -- Select --
  • Paragraph 1
  • Point 2
  • Point 3
-- Select -- Item 1 Item 2 Item 3

CSS classes that can be used to design a select

To create selectivity for additional CSS, use the following classes:

.selectboxFather's container for all selection
.selectbox .selectSelect from the burning camp
.selectbox.focused .selectFocus on select when Tab key is pressed
.selectbox .select .textattachments tag for the selected option to insert a background image using the “open doors” technique
.selectbox .triggerrights to part of the burned select (smart remixer)
.selectbox .trigger .arrowattachment tag for remix (arrow)
.selectbox .dropdownburner for the list
.selectbox .dropdown ulrandom list
.selectbox liitem (option) select
.selectbox li.selectedvibranium select item
.selectbox li.disabledvimkneny (not available for selection) select item
Visnovok

The creation of such a script is enough to keep you busy, while you have to deal with a lot of different things. I am sure that there will be no more serious bugs. Hello, let me know in the comments.

© 2024 androidas.ru - All about Android