Custom select (Nice select) with custom scrollbar (mCustomScrollbar) - javascript
I'm new here and I'm facing an issue which I can't fix.
I have a custom select using nice select which is working perfectly, then I added mCustomScrollbar which is working fine too, the issue is when I want to use the keyboard only, I first focus using TAB and then pressing the down arrow (or spacebar, enter, up arrow) where it displays the options, once you keep using the arrow to move down the scrollbar is not moving with the cursor showing the rest of the options like if you use the mouse, I would like to find a way of fixing this so that when a user is using the keyboard it can see all the options.
Codepen: https://codepen.io/anon/pen/MPPybJ
HTML
<section class="form-container">
<form action="" method="" class="tn-general" novalidate>
<fieldset>
<div class="row">
<div class="col-md-5">
<div class="form-group form-group-floating-label">
<div class="custom-select">
<select required>
<option value="" disabled selected="selected" hidden>Seleccionar</option>
<option value="1">Soltero</option>
<option value="2">Casado</option>
<option value="3">Divorciado</option>
<option value="4">Viudo</option>
<option value="5">Concubino</option>
<option value="">Casado 1</option>
<option value="">Casado 2</option>
<option value="">Casado 3</option>
<option value="">Casado 4</option>
<option value="">Casado 5</option>
<option value="">Casado 6</option>
<option value="">Casado 7</option>
<option value="">Casado 8</option>
<option value="">Casado 9</option>
<option value="">Casado 10</option>
<option value="">Casado 11</option>
<option value="">Casado 12</option>
<option value="">Casado 13</option>
<option value="">Casado 14</option>
<option value="">Casado 15</option>
<option value="">Casado 16</option>
<option value="">Casado 17</option>
<option value="">Casado 18</option>
<option value="">Casado 19</option>
<option value="">Casado 20</option>
</select>
</div>
</div>
</div>
</div>
</fieldset>
</form>
</section>
CSS
/* Font Size*/
$font-size-small: 0.8em; /*12px*/
$font-size-minismall: 0.813em; /*13px*/
$font-size-normal: 0.9em; /* 14px */
$font-size-main-mininormal: 0.938em; /* 15px */
$font-size-main-normal: 1em; /* 16px */
$font-size-smedium: 1.115em; /* 17px */
$font-size-medium: 1.125em; /* 18px */
$font-size-xmedium: 1.25em; /* 20px */
$font-size-xm-medium: 1.375em; /* 22px */
$font-size-big: 1.5em; /*24px*/
$font-size-xbig: 1.65em; /* 26px */
$font-size-mxlarge: 1.9em; /* 30px */
$font-size-mxxlarge: 2em; /* 32px */
$font-size-mxxxlarge: 2.125em; /* 34px */
$font-size-xlarge: 2.5em; /* 40px */
$font-size-large: 2.75em; /*44px*/
$font-size-xxlarge: 3em; /*48px*/
$font-size-xxxlarge: 50px; /* 50px */
$font-size-ultralarge: 3.75em; /* 60px */
/* Font Type*/
$font-type: "opensans-regular";
$font-type-light: "opensans-light";
$font-type-semibold: "opensans-semibold";
$font-type-bold: "opensans-bold";
$font-type-2: "urwgeometric-regular";
$font-type-2-light: "urwgeometric-light";
$font-type-2-thin: "urwgeometric-thin";
$font-type-2-semibold: "urwgeometric-semibold";
$font-type-2-bold: "urwgeometric-bold";
$color-white: #ffffff;
$color-black: #000000;
/* Gray Color*/
$color-gray: #eeeeee;
$color-gray-1:#C8C8C8;
$color-message-gray: #999fa5;
$color-soft-gray: #aeaeae;
$color-soft-gray-2: #f3f3f3;
$color-soft-gray-3: #c1c1c1;
$color-soft-gray-4: #aaa;
$color-soft-gray-5: #777777;
$color-soft-gray-6: #969696;
$color-soft-gray-7: #f9f9f9;
$color-soft-gray-8: #dddddd;
$color-soft-gray-9: #f0f0f0;
$color-soft-gray-10: #e3e3e3;
$color-dark-gray: #666666;
$color-dark-gray-0: #333333;
$color-dark-gray-2: #434343;
$color-dark-gray-3: #535353;
$color-dark-gray-4: #626262;
$color-dark-gray-5: #d4d4d4;
$color-dark-gray-5: #c9c9c9;
$color-dark-gray-6: #a6a6a6;
$color-dark-gray-7: #555555;
$color-dark-gray-8: #303030;
$color-dark-gray-9:#d8d8d8;
/* Orange Color*/
$color-soft-orange: #f9a885;
$color-super-soft-orange: #FFF8E4;
$color-ui-orange: #ff6600;
$color-ui-orange-2: #ff5a00;
$color-ui-orange-3: #FF6800;
$color-orange: #f56122;
$color-orange-2:#ef5816;
$color-orange-3:#ed5927;
$color-orange-3: #ff7022;
$color-orange-4: #ff5d00;
$color-orange-5: #ef5411;
$color-orange-6:#ff8b1d;
$color-orange-7:#ff8400;
$color-orange-8: #FFA500;
$color-dark-orange: #da480a;
/* Red Color*/
$color-red: #e14421;
$color-red-2: #de0000;
$color-red-3 :#ff6464;
/* Blue Color*/
$color-blue: #3F525F;
$color-dark-blue: #2c4854;
/* Green Color*/
$color-green: #009a63;
$color-green-2: #009900;
$color-green-3: #00d095;
/*Yellow Colors*/
$color-soft-yellow: #fcf8e3;
$color-soft-yellow-2: #fff5d8;
$color-yellow-1: #ffbf00;
$color-yellow-2: #fad054;
/*Gradient Class Colors*/
.tn-gradient-1 {
background-image: linear-gradient(119deg, #ff6600,#ffcb00);
}
.tn-general {
.custom-checkbox {
.custom-control-indicator {
border-radius: 50%;
border: 0.15rem solid $color-soft-gray-3;
background-color: white;
width: 1.5rem;
height: 1.5rem;
}
.custom-control-description {
padding-top: 5px;
padding-left: 10px;
}
.custom-control-input:checked~.custom-control-indicator {
color: white;
background-color: $color-ui-orange;
border-color: $color-ui-orange;
background-size: 10px;
}
.custom-control-input:focus ~ .custom-control-indicator {
box-shadow: 0 0 0 1px #fff, 0 0 0 3px #fff;
}
}
.custom-checkbox-nobg {
.custom-control-indicator {
border-radius: 25%;
border: 0.05rem solid $color-soft-gray;
background-color: white;
width: 1.5rem;
height: 1.5rem;
}
.custom-control-description {
padding-top: 5px;
padding-left: 10px;
}
.custom-control-input:checked ~ .custom-control-indicator {
color: $color-dark-gray;
background-color: white;
border-color: $color-dark-gray;
background-size: 10px;
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath fill='%23666' d='M6.564.75l-3.59 3.612-1.538-1.55L0 4.26 2.974 7.25 8 2.193z'/%3E%3C/svg%3E");
}
.custom-control-input:focus ~ .custom-control-indicator {
box-shadow: 0 0 0 1px #fff, 0 0 0 3px #fff;
}
}
.tn-btn-main {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
cursor: pointer;
background-color: $color-orange-3;
border-radius: 50px;
border: 0;
color: white;
font-family: $font-type-semibold;
font-size: $font-size-main-normal;
line-height: 1;
text-align: center;
width: 200px;
height: 42px;
padding: 0;
margin: 20px 0;
display: inline-block;
vertical-align: middle;
transform: perspective(1px) translateZ(0);
box-shadow: 0 0 1px transparent;
overflow: hidden;
transition-duration: 0.3s;
transition-property: color, background-color;
&.tn-mt-3 {
margin-top: 30px;
}
&:hover {
background-color: $color-orange-7;
}
&:focus {
outline: 0;
}
&.disabled {
opacity: 0.6;
color: $color-dark-gray-7;
cursor: default;
font-family: "opensans-regular";
}
}
.tn-btn-msg {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
cursor: pointer;
background-color: $color-ui-orange-3;
border-radius: 5px;
border: 0;
color: white;
font-family: $font-type-semibold;
font-size: $font-size-medium;
line-height: 1;
padding: 10px 15px;
margin: 5px 0;
display: inline-block;
vertical-align: middle;
transform: perspective(1px) translateZ(0);
box-shadow: 0 0 1px transparent;
overflow: hidden;
transition-duration: 0.3s;
transition-property: color, background-color;
&.tn-mt-3 {
margin-top: 30px;
}
&:hover {
background-color: $color-orange-7;
}
&:focus {
outline: 0;
}
&.disabled {
opacity: 0.6;
color: $color-dark-gray-7;
cursor: default;
font-family: "opensans-regular";
}
}
.tn-btn-second {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
cursor: pointer;
background-color: $color-white;
border: 2px solid $color-ui-orange;
border-radius: 50px;
color: $color-ui-orange;
font-family: $font-type-semibold;
font-size: $font-size-main-normal;
line-height: 1;
width: 200px;
height: 42px;
padding: 0;
text-align: center;
margin: 20px 0;
display: inline-block;
vertical-align: middle;
transform: perspective(1px) translateZ(0);
box-shadow: 0 0 1px transparent;
overflow: hidden;
transition-duration: 0.3s;
transition-property: color, background-color;
&.tn-mt-3 {
margin-top: 30px;
}
&:hover {
border: 2px solid $color-orange-7;
background-color: $color-white;
color: $color-orange-7;
}
&:focus {
outline: 0;
}
&.disabled {
opacity: 0.6;
color: $color-dark-gray-7;
cursor: default;
font-family: "opensans-regular";
}
}
.tn-btn-collapse {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
color: $color-orange-5;
font-size: $font-size-normal;
font-family: $font-type-semibold;
background-color: $color-soft-gray-9;
width: 100%;
border: 0 none;
padding: 0.8rem;
position: relative;
&:before {
outline: 0;
display: inline-block;
content: "";
background: url("../images/buttons/tn-arrow-collapse.png") center center no-repeat;
height: 12px;
width: 12px;
position: relative;
top: 2px;
right: 5px;
}
&:focus {
outline: 0;
box-shadow: none;
}
}
.tn-btn-collapse[aria-expanded="true"] {
&:before {
background: url("../images/buttons/tn-arrow-collapsed.png") no-repeat;
}
}
/* Black Check */
.check-icon {
list-style-type: none;
li {
background: url("../images/icons/check-small-2.png") no-repeat 0 4px;
padding-left: 20px;
margin: 5px 0;
font-family: $font-type;
line-height: 1;
color: $color-dark-gray;
}
}
.tn-form-wrapper {
p {
margin-bottom: 0;
}
}
.tn-form-title {
h1 {
color: $color-dark-gray-8;
font-family: $font-type;
font-weight: bold;
font-size: $font-size-mxxxlarge;
padding-bottom: 15px;
}
}
// Floating labels
.form-group.form-group-floating-label {
position: relative;
&:not(:first-child) {
margin-top: 40px;
}
label {
font-size: 15px;
font-weight: 600;
color: $color-soft-gray-4;
display: block;
}
.form-control {
font-family: 'urwgeometric-regular';
font-size: $font-size-xmedium;
font-weight: 600;
color: $color-dark-gray-0;
width: 100%;
border: 0;
border-bottom: 1px solid #CCC;
border-radius: 0;
padding-top: 0;
padding-left: 0;
outline: 0;
&.invalid {
border-color: $color-red-2;
}
&:focus {
border-color: $color-ui-orange;
}
&:focus + .form-control-placeholder,
&:valid + .form-control-placeholder {
font-size: $font-size-main-normal;
transform: translate3d(0, -100%, 0);
}
&:disabled + .form-control-placeholder {
font-size: $font-size-main-normal;
transform: translate3d(0, -100%, 0);
}
&:disabled + .form-control-placeholder.form-control-placeholder-no-input {
transform: none;
}
&:disabled {
background-color: transparent;
cursor: not-allowed;
border-color: $color-soft-gray-10;
}
}
.form-control-placeholder {
font-size: $font-size-xmedium;
font-weight: 600;
color: $color-soft-gray-4;
position: absolute;
top: 0;
left: 0;
padding-top: 5px;
transition: all 200ms;
cursor: text;
}
.form-invalid-message {
font-size: $font-size-normal;
font-weight: 600;
color: $color-red-2;
max-width: 100%;
margin-top: 4px;
margin-bottom: 0;
position: absolute;
&.position-relative {
position: relative;
}
}
&.form-group-single-inputs {
input {
display: inline-block;
height: 24px;
font-size: $font-size-xbig;
text-align: center;
max-width: 20px;
padding: 0;
margin-top: 17px;
margin-right: 7px;
vertical-align: bottom;
&:valid {
border-color: transparent;
}
&:invalid {
border-color: #CCC;
}
&:focus {
border-color: $color-ui-orange;
}
&.invalid {
border-color: $color-red-2;
}
}
}
// custom select dropdown
select {
display: none;
}
.custom-select {
display: block;
background: transparent;
border: 0;
padding: 6px 0;
position: relative;
.nice-select {
font-family: 'urwgeometric-regular';
font-size: $font-size-xmedium;
font-weight: 600;
color: $color-dark-gray-0;
background: url('../../Content/images/buttons/chevron-down-arrow-light-gray.png') no-repeat right;
background-size: 15px;
border-bottom: 1px solid #CCC;
padding-bottom: 7px;
cursor: pointer;
&:focus {
outline-width: 2px;
outline-style: solid;
outline-color: Highlight;
}
&:active,
&:hover,
&.removeFocus {
outline: none;
}
& span.current {
color: $color-soft-gray-4;
}
&.open {
border-color: $color-ui-orange;
.list {
opacity: 1;
pointer-events: auto;
transform: scale(1) translateY(0);
}
}
&.invalid {
border-color: $color-red-2;
}
&.disabled {
border-color: $color-soft-gray-10;
pointer-events: none;
& span {
color: $color-soft-gray-10;
}
}
&.optionSelected {
background: url('../../Content/images/buttons/chevron-down-arrow-dark-gray.png') no-repeat right;
background-size: 15px;
& span.current {
color: $color-dark-gray-0;
}
}
// List and options
.list {
max-height: 400px;
font-size: $font-size-medium;
font-weight: 600;
background-color: white;
color: $color-dark-gray-0;
position: absolute;
top: 100%;
left: 0;
right: 0;
z-index: 1;
margin-top: 8px;
border: 1px solid #e5e5e5;
border-radius: 6px;
box-shadow: 0 5px 10px 0 rgba(0, 0, 0, 0.1);
opacity: 0;
pointer-events: none;
overflow: hidden;
transition: all .2s cubic-bezier(0.5, 0, 0, 1.25), opacity .15s ease-out;
&:hover .option:not(:hover):not(.selected) {
background-color: transparent;
}
}
.option {
font-weight: bold;
font-size: 18px;
height: 70px;
padding: 0 20px;
display: flex;
align-items: center;
cursor: pointer;
outline: none;
transition: all 0.2s;
&:hover,
&.focus,
&.selected.focus {
background-color: $color-gray;
}
&.selected {
background-color: $color-gray;
}
&.disabled {
display: none;
}
}
}
}
// Input checkbox
.form-input-checkbox {
// Remove the original ones
.styled-checkbox {
position: absolute;
opacity: 0;
// Checbox label
& + label {
display: inline;
position: relative;
padding: 0;
cursor: pointer;
// Add styled chexbox
&:before {
content: '';
display: inline-block;
width: 22px;
height: 22px;
background: white;
vertical-align: text-bottom;
margin-right: 18px;
border: 2px solid $color-dark-gray-0;
border-radius: 4px;
}
}
// Box checked
&:checked + label:before {
content: '';
background: url('../../Content/images/icons/select-checkbox-checked.png') no-repeat right;
background-size: contain;
border: 0;
border-radius: 0;
}
}
}
.custom-radio {
& .custom-control-label {
cursor: pointer;
margin-bottom: 0;
&::before {
content: '';
border-radius: 50%;
display: inline-block;
width: 24px;
height: 24px;
pointer-events: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background: url('../../Content/images/icons/radio-button-unselected.svg') center center no-repeat;
vertical-align: top;
cursor: pointer;
margin-right: 10px;
}
&::after {
content: '';
display: block;
width: 24px;
height: 24px;
background-repeat: no-repeat;
background-position: center center;
background-size: 50% 50%;
position: absolute;
top: 0;
}
}
& .custom-control-input:not(:checked):focus ~ .custom-control-label::before {
outline-width: 2px;
outline-style: solid;
outline-color: Highlight;
}
& .custom-control-input:checked ~ .custom-control-label::before {
background: url('../../Content/images/icons/radio-button-selected.svg') center center no-repeat;
}
& .custom-control-input:checked ~ .custom-control-label::after {
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3E%3Ccircle r='3' fill='%23fff'/%3E%3C/svg%3E");
}
}
// input[type="radio"] {
// & + label span {
// display: inline-block;
// width: 24px;
// height: 24px;
// vertical-align: top;
// background: url('../../Content/images/icons/radio-button-unselected.svg') center center no-repeat;
// cursor: pointer;
// margin-right: 10px;
// }
//
// &:checked + label span {
// background: url('../../Content/images/icons/radio-button-selected.svg') center center no-repeat;
// }
// }
}
// General errors
.general-invalid-data {
font-size: 15px;
font-weight: 600;
line-height: 1;
color: $color-red-2;
}
// Remove box-shadow from inputs with type text
input[type="text"] {
box-shadow: none;
}
}
JS
// initialize custom select
$('select').niceSelect();
// add outline only when using TAB
let niceSelect = $('.nice-select');
niceSelect.on('click', function()
{
$(this).addClass('removeFocus');
});
niceSelect.on('blur', function()
{
if($(this).hasClass('removeFocus'))
{
$(this).removeClass('removeFocus');
}
});
// add class when choosing an option
$('.list .option:not(.disabled)').on('click keypress', function()
{
$(this).closest('.nice-select').addClass('optionSelected');
});
// remove mCustomScrollbar tabindex
setTimeout(() => {
$('.mCustomScrollBox').prop('tabindex', '-1');
}, 1000);
$('.nice-select .list').mCustomScrollbar({
theme: 'minimal-dark'
});
Thank you!
I had same task. And I did it like this
script:
$('.jsNiceSelect').niceSelect();
$(document).on('mouseenter', '.nice-select .mCSB_scrollTools', function(event) {
var $dropdown = $(this).parents('.nice-select');
$dropdown.addClass('open_scroll');
});
$(document).on('click.nice_select', function(event) {
if ($(event.target).closest('.nice-select').length === 0) {
$('.nice-select').removeClass('open_scroll');
setTimeout(function() { $('.nice-select').removeClass('open'); }, 50);
}
});
$(document).on('click.nice_select', '.nice-select .option:not(.disabled)', function(event) {
$('.nice-select').removeClass('open_scroll open');
setTimeout(function() { $('.nice-select').removeClass('open'); }, 50);
});
css:
...
&.open {
here standard styles nice select
}
here add this:
&.open_scroll{
z-index: 4;
&:after {
-webkit-transform: rotate(180deg);
-ms-transform: rotate(180deg);
transform: rotate(180deg);
}
.list {
opacity: 1;
pointer-events: auto;
-webkit-transform: scale(1) translateY(0);
-ms-transform: scale(1) translateY(0);
transform: scale(1) translateY(0);
}
}
...
Related
How to target and display error message with data-error in JavaScript
I'de like to validate a form with data propriety data-error and data-error-visible. I want to display the error message only with JS and don't use anymore HTML codes to do it. The current code I use is below and I left only the first input : /****** * FORM VALIDATION * ******/ // DOM const formData = document.querySelectorAll(".formData"); const form = document.getElementById("form"); const firstName = document.getElementById("firstName"); const lastName = document.getElementById("lastName"); const email = document.getElementById("email"); const birthdate = document.getElementById("birthdate"); const quantity = document.getElementById("quantity"); const tournament = document.getElementsByName("location"); const checkbox = document.getElementsByName("checkbox"); // form preventDefault + checkInputs function form.addEventListener('submit', e => { e.preventDefault(); }); /*********** * DATA-ATTRIBUTE * ************/ // set error message to "true" const displayErrorMsg = (element, message) => { const formData = element formData.setAttribute("data-error", message) formData.setAttribute("data-error-visible", true) } // Function for remove one error msg const removeErrorMsg = (element) => { const formData = element formData.removeAttribute('data-error') formData.removeAttribute('data-error-visible') } /*********** * * VALIDATION FUNCTION * ************/ function validate(formData) { let isFormValid = true; // Regex const nameRegex = /[a-zA-Z]+/i; // First name if (firstName.value.match(nameRegex) || firstName.value.length < 2) { isFormValid = false; displayErrorMsg(firstName); } else { removeErrorMsg(firstName); } } :root { --font-default: "DM Sans", Arial, Helvetica, sans-serif; --font-slab: var(--font-default); --modal-duration: 0.8s; } * { box-sizing: border-box; margin: 0; padding: 0; } /* Landing Page */ body { margin: 0; display: flex; flex-direction: column; font-family: var(--font-default); font-size: 18px; max-width: 1300px; margin: 0 auto; } p { margin-bottom: 0; padding: 0.5vw; } img { padding-right: 1rem; } .topnav { overflow: hidden; margin: 3.5%; } .header-logo { float: left; } .main-navbar { float: right; } .topnav a { float: left; display: block; color: #000000; text-align: center; padding: 12px 12px; margin: 5px; text-decoration: none; font-size: 20px; font-family: Roboto, sans-serif; } .topnav a:hover { background-color: #ff0000; color: #ffffff; border-radius: 15px; } .topnav a.active { background-color: #ff0000; color: #ffffff; border-radius: 15px; } .topnav .icon { display: none; } #media screen and (max-width: 768px) { .topnav a {display: none;} .topnav a.icon { float: right; display: block; } } #media screen and (max-width: 768px) { .topnav.responsive {position: relative;} .topnav.responsive .icon { position: absolute; right: 0; top: 0; } .topnav.responsive a { float: none; display: block; text-align: left; } } #media screen and (max-width: 540px) { .topnav a {display: none;} .topnav a.icon { float: right; display: block; margin-top: -15px; } } main { font-size: 130%; font-weight: bolder; color: black; padding-top: 0.5vw; padding-left: 2vw; padding-right: 2vw; margin: 1px 20px 15px; border-radius: 2rem; text-align: justify; } .modal-btn { font-size: 145%; background: #fe142f; display: flex; margin: auto; padding: 2em; color: #fff; padding: 0.75rem 2.5rem; border-radius: 1rem; cursor: pointer; } .modal-btn:hover { background: #3876ac; } .footer { margin: 20px; padding: 10px; font-family: var(--font-slab); } /* Modal form */ .button { background: #fe142f; margin-top: 0.5em; padding: 1em; color: #fff; border-radius: 15px; cursor: pointer; font-size: 16px; } .button:hover { background: #3876ac; } .smFont { font-size: 16px; } .bground { display: none; position: fixed; z-index: 1; left: 0; top: 0; height: 100%; width: 100%; overflow: auto; background-color: rgba(26, 39, 156, 0.4); } .content { margin: 5% auto; width: 100%; max-width: 500px; animation-name: modalopen; animation-duration: var(--modal-duration); background: #232323; border-radius: 10px; overflow: hidden; position: relative; color: #fff; padding-top: 10px; } .modal-body { padding: 15px 8%; margin: 15px auto; } label { font-family: var(--font-default); font-size: 17px; font-weight: normal; display: inline-block; margin-bottom: 11px; } input { padding: 8px; border: 0.8px solid #ccc; outline: none; } .text-control { margin: 0; padding: 8px; width: 100%; border-radius: 8px; font-size: 20px; height: 48px; } .formData[data-error]::after { content: attr(data-error); font-size: 0.4em; color: #e54858; display: block; margin-top: 7px; margin-bottom: 7px; text-align: right; line-height: 0.3; opacity: 0; transition: 0.3s; } .formData[data-error-visible="true"]::after { opacity: 1; } .formData[data-error-visible="true"] .text-control { border: 2px solid #e54858; } /* input[data-error]::after { content: attr(data-error); font-size: 0.4em; color: red; } */ .checkbox-label, .checkbox2-label { position: relative; margin-left: 36px; font-size: 12px; font-weight: normal; } .checkbox-label .checkbox-icon, .checkbox2-label .checkbox-icon { display: block; width: 20px; height: 20px; border: 2px solid #279e7a; border-radius: 50%; text-indent: 29px; white-space: nowrap; position: absolute; left: -30px; top: -1px; transition: 0.3s; } .checkbox-label .checkbox-icon::after, .checkbox2-label .checkbox-icon::after { content: ""; width: 13px; height: 13px; background-color: #279e7a; border-radius: 50%; text-indent: 29px; white-space: nowrap; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); transition: 0.3s; opacity: 0; } .checkbox-input { display: none; } .checkbox-input:checked + .checkbox-label .checkbox-icon::after, .checkbox-input:checked + .checkbox2-label .checkbox-icon::after { opacity: 1; } .checkbox-input:checked + .checkbox2-label .checkbox-icon { background: #279e7a; } .checkbox2-label .checkbox-icon { border-radius: 4px; border: 0; background: #c4c4c4; } .checkbox2-label .checkbox-icon::after { width: 7px; height: 4px; border-radius: 2px; background: transparent; border: 2px solid transparent; border-bottom-color: #fff; border-left-color: #fff; transform: rotate(-55deg); left: 21%; top: 19%; } .close { position: absolute; right: 15px; top: 15px; width: 32px; height: 32px; opacity: 1; cursor: pointer; transform: scale(0.7); } .close:before, .close:after { position: absolute; left: 15px; content: " "; height: 33px; width: 3px; background-color: #fff; } .close:before { transform: rotate(45deg); } .close:after { transform: rotate(-45deg); } .btn-submit, .btn-signup { background: #fe142f; display: block; margin: 0 auto; border-radius: 7px; font-size: 1rem; padding: 12px 82px; color: #fff; cursor: pointer; border: 0; } /* custom select styles */ .custom-select { position: relative; font-family: Arial; font-size: 1.1rem; font-weight: normal; } .custom-select select { display: none; } .select-selected { background-color: #fff; } /* Style the arrow inside the select element: */ .select-selected:after { position: absolute; content: ""; top: 10px; right: 13px; width: 11px; height: 11px; border: 3px solid transparent; border-bottom-color: #f00; border-left-color: #f00; transform: rotate(-48deg); border-radius: 5px 0 5px 0; } /* Point the arrow upwards when the select box is open (active): */ .select-selected.select-arrow-active:after { transform: rotate(135deg); top: 13px; } .select-items div, .select-selected { color: #000; padding: 11px 16px; border: 1px solid transparent; border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent; cursor: pointer; border-radius: 8px; height: 48px; } /* Style items (options): */ .select-items { position: absolute; background-color: #fff; top: 89%; left: 0; right: 0; z-index: 99; } /* Hide the items when the select box is closed: */ .select-hide { display: none; } .select-items div:hover, .same-as-selected { background-color: rgba(0, 0, 0, 0.1); } /* custom select end */ .text-label { font-weight: normal; font-size: 16px; } .hero-section { min-height: 93vh; border-radius: 10px; display: grid; grid-template-columns: repeat(12, 1fr); overflow: hidden; box-shadow: 0 2px 7px 2px rgba(0, 0, 0, 0.2); margin-bottom: 10px; } .hero-content { padding: 51px 67px; grid-column: span 4; background: #232323; color: #fff; position: relative; text-align: left; min-width: 424px; } .hero-content::after { content: ""; width: 100%; height: 100%; background: #232323; position: absolute; right: -80px; top: 0; } .hero-content > * { position: relative; z-index: 1; } .hero-headline { font-size: 6rem; font-weight: normal; white-space: nowrap; } .hero-text { width: 146%; font-weight: normal; margin-top: 57px; padding: 0; } .btn-signup { outline: none; text-transform: capitalize; font-size: 1.3rem; padding: 15px 23px; margin: 0; margin-top: 59px; } .hero-img { grid-column: span 8; } .hero-img img { width: 100%; height: 100%; display: block; padding: 0; } .copyrights { color: #fe142f; padding: 0; font-size: 1rem; margin: 60px 0 30px; font-weight: bolder; } .hero-section > .btn-signup { display: none; } footer { padding-left: 2vw; padding-right: 2vw; margin: 0 20px; } #media screen and (max-width: 800px) { .hero-section { display: block; box-shadow: unset; } .hero-content { background: #fff; color: #000; padding: 20px; } .hero-content::after { content: unset; } .hero-content .btn-signup { display: none; } .hero-headline { font-size: 4.5rem; white-space: normal; } .hero-text { width: unset; font-size: 1.5rem; } .hero-img img { border-radius: 10px; margin-top: 40px; } .hero-section > .btn-signup { display: block; margin: 32px auto 10px; padding: 12px 35px; } .copyrights { margin-top: 50px; text-align: center; } } #keyframes modalopen { from { opacity: 0; transform: translateY(-150px); } to { opacity: 1; } } <form id="form" name="reserve" action="index.html" method="get" > <div class="formData"> <label for="firstName">Prénom</label><br> <input class="text-control" type="text" id="firstName" name="first" minlength="2" required /><br> </div> <input class="btn-submit" type="submit" class="button" value="C'est parti" /> </form>
Getting Invalid LatLng object when trying to map coordinates using leaflet
I'm trying to figure out how I can loop each of the coordinates, <div class="map-container" id="mapid"></div> <ul class="plot"> <li class="markers" data-lattitude="-40.99497" data-longitude="174.50808">Text 1</li> <li class="markers" data-lattitude="-41.30269" data-longitude="173.63696">Text 2</li> <li class="markers" data-lattitude="-41.49413" data-longitude="173.5421">Text 3</li> <li class="markers" data-lattitude="-40.98585" data-longitude="174.50659">Text 4</li> <li class="markers" data-lattitude="-40.93163" data-longitude="173.81726">Text 5</li> </ul> add them to an array, plot them on the map with the popup text shown. Below is an error that I can't seem to solve. not sure which part of the code is wrong. leaflet.js:5 Uncaught Error: Invalid LatLng object: (e, x) Below is the sample of the code that I have been working on. var map = L.map('mapid', { fullscreenControl: { pseudoFullscreen: false // if true, fullscreen to page width and height }, }).setView([30.539255791073, 58.383450508118], 5); map.createPane('labels'); map.getPane('labels').style.zIndex = 650; map.getPane('labels').style.pointerEvents = 'none'; var positron = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { noWrap: true, maxZoom : 10 }).addTo(map); var positronLabels = L.tileLayer('http://{s}.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}.png', { pane: 'labels', noWrap: true, maxZoom : 3 }).addTo(map); var southWest = L.latLng(-89.98155760646617, -180), northEast = L.latLng(89.99346179538875, 180), bounds = L.latLngBounds(southWest, northEast); map.setMaxBounds(bounds); map.on('drag', function() { map.panInsideBounds(bounds, { animate: false }); }); var plot = []; $('.plot > .markers').each(function() { plot.push($(this).text(), $(this).data('lattitude'), $(this).data('longitude') ); }); console.log( plot ); for ( var i = 0; i < plot.length; i++ ) { marker = new L.marker([plot[i][1], plot[i][2]]).bindPopup(plot[i][0], {autoClose:false}).addTo(map).openPopup(); } /* required styles */ .leaflet-pane, .leaflet-tile, .leaflet-marker-icon, .leaflet-marker-shadow, .leaflet-tile-container, .leaflet-pane > svg, .leaflet-pane > canvas, .leaflet-zoom-box, .leaflet-image-layer, .leaflet-layer { position: absolute; left: 0; top: 0; } .leaflet-container { overflow: hidden; } .leaflet-tile, .leaflet-marker-icon, .leaflet-marker-shadow { -webkit-user-select: none; -moz-user-select: none; user-select: none; -webkit-user-drag: none; } /* Safari renders non-retina tile on retina better with this, but Chrome is worse */ .leaflet-safari .leaflet-tile { image-rendering: -webkit-optimize-contrast; } /* hack that prevents hw layers "stretching" when loading new tiles */ .leaflet-safari .leaflet-tile-container { width: 1600px; height: 1600px; -webkit-transform-origin: 0 0; } .leaflet-marker-icon, .leaflet-marker-shadow { display: block; } /* .leaflet-container svg: reset svg max-width decleration shipped in Joomla! (joomla.org) 3.x */ /* .leaflet-container img: map is broken in FF if you have max-width: 100% on tiles */ .leaflet-container .leaflet-overlay-pane svg, .leaflet-container .leaflet-marker-pane img, .leaflet-container .leaflet-shadow-pane img, .leaflet-container .leaflet-tile-pane img, .leaflet-container img.leaflet-image-layer, .leaflet-container .leaflet-tile { max-width: none !important; max-height: none !important; } .leaflet-container.leaflet-touch-zoom { -ms-touch-action: pan-x pan-y; touch-action: pan-x pan-y; } .leaflet-container.leaflet-touch-drag { -ms-touch-action: pinch-zoom; /* Fallback for FF which doesn't support pinch-zoom */ touch-action: none; touch-action: pinch-zoom; } .leaflet-container.leaflet-touch-drag.leaflet-touch-zoom { -ms-touch-action: none; touch-action: none; } .leaflet-container { -webkit-tap-highlight-color: transparent; } .leaflet-container a { -webkit-tap-highlight-color: rgba(51, 181, 229, 0.4); } .leaflet-tile { filter: inherit; visibility: hidden; } .leaflet-tile-loaded { visibility: inherit; } .leaflet-zoom-box { width: 0; height: 0; -moz-box-sizing: border-box; box-sizing: border-box; z-index: 800; } /* workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=888319 */ .leaflet-overlay-pane svg { -moz-user-select: none; } .leaflet-pane { z-index: 400; } .leaflet-tile-pane { z-index: 200; } .leaflet-overlay-pane { z-index: 400; } .leaflet-shadow-pane { z-index: 500; } .leaflet-marker-pane { z-index: 600; } .leaflet-tooltip-pane { z-index: 650; } .leaflet-popup-pane { z-index: 700; } .leaflet-map-pane canvas { z-index: 100; } .leaflet-map-pane svg { z-index: 200; } .leaflet-vml-shape { width: 1px; height: 1px; } .lvml { behavior: url(#default#VML); display: inline-block; position: absolute; } /* control positioning */ .leaflet-control { position: relative; z-index: 800; pointer-events: visiblePainted; /* IE 9-10 doesn't have auto */ pointer-events: auto; } .leaflet-top, .leaflet-bottom { position: absolute; z-index: 1000; pointer-events: none; } .leaflet-top { top: 0; } .leaflet-right { right: 0; } .leaflet-bottom { bottom: 0; } .leaflet-left { left: 0; } .leaflet-control { float: left; clear: both; } .leaflet-right .leaflet-control { float: right; } .leaflet-top .leaflet-control { margin-top: 10px; } .leaflet-bottom .leaflet-control { margin-bottom: 10px; } .leaflet-left .leaflet-control { margin-left: 10px; } .leaflet-right .leaflet-control { margin-right: 10px; } /* zoom and fade animations */ .leaflet-fade-anim .leaflet-tile { will-change: opacity; } .leaflet-fade-anim .leaflet-popup { opacity: 0; -webkit-transition: opacity 0.2s linear; -moz-transition: opacity 0.2s linear; transition: opacity 0.2s linear; } .leaflet-fade-anim .leaflet-map-pane .leaflet-popup { opacity: 1; } .leaflet-zoom-animated { -webkit-transform-origin: 0 0; -ms-transform-origin: 0 0; transform-origin: 0 0; } .leaflet-zoom-anim .leaflet-zoom-animated { will-change: transform; } .leaflet-zoom-anim .leaflet-zoom-animated { -webkit-transition: -webkit-transform 0.25s cubic-bezier(0,0,0.25,1); -moz-transition: -moz-transform 0.25s cubic-bezier(0,0,0.25,1); transition: transform 0.25s cubic-bezier(0,0,0.25,1); } .leaflet-zoom-anim .leaflet-tile, .leaflet-pan-anim .leaflet-tile { -webkit-transition: none; -moz-transition: none; transition: none; } .leaflet-zoom-anim .leaflet-zoom-hide { visibility: hidden; } /* cursors */ .leaflet-interactive { cursor: pointer; } .leaflet-grab { cursor: -webkit-grab; cursor: -moz-grab; cursor: grab; } .leaflet-crosshair, .leaflet-crosshair .leaflet-interactive { cursor: crosshair; } .leaflet-popup-pane, .leaflet-control { cursor: auto; } .leaflet-dragging .leaflet-grab, .leaflet-dragging .leaflet-grab .leaflet-interactive, .leaflet-dragging .leaflet-marker-draggable { cursor: move; cursor: -webkit-grabbing; cursor: -moz-grabbing; cursor: grabbing; } /* marker & overlays interactivity */ .leaflet-marker-icon, .leaflet-marker-shadow, .leaflet-image-layer, .leaflet-pane > svg path, .leaflet-tile-container { pointer-events: none; } .leaflet-marker-icon.leaflet-interactive, .leaflet-image-layer.leaflet-interactive, .leaflet-pane > svg path.leaflet-interactive { pointer-events: visiblePainted; /* IE 9-10 doesn't have auto */ pointer-events: auto; } /* visual tweaks */ .leaflet-container { background: #ddd; outline: 0; } .leaflet-container a { color: #0078A8; } .leaflet-container a.leaflet-active { outline: 2px solid orange; } .leaflet-zoom-box { border: 2px dotted #38f; background: rgba(255,255,255,0.5); } /* general typography */ .leaflet-container { font: 12px/1.5 "Helvetica Neue", Arial, Helvetica, sans-serif; } /* general toolbar styles */ .leaflet-bar { box-shadow: 0 1px 5px rgba(0,0,0,0.65); border-radius: 4px; } .leaflet-bar a, .leaflet-bar a:hover { background-color: #fff; border-bottom: 1px solid #ccc; width: 26px; height: 26px; line-height: 26px; display: block; text-align: center; text-decoration: none; color: black; } .leaflet-bar a, .leaflet-control-layers-toggle { background-position: 50% 50%; background-repeat: no-repeat; display: block; } .leaflet-bar a:hover { background-color: #f4f4f4; } .leaflet-bar a:first-child { border-top-left-radius: 4px; border-top-right-radius: 4px; } .leaflet-bar a:last-child { border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-bottom: none; } .leaflet-bar a.leaflet-disabled { cursor: default; background-color: #f4f4f4; color: #bbb; } .leaflet-touch .leaflet-bar a { width: 30px; height: 30px; line-height: 30px; } .leaflet-touch .leaflet-bar a:first-child { border-top-left-radius: 2px; border-top-right-radius: 2px; } .leaflet-touch .leaflet-bar a:last-child { border-bottom-left-radius: 2px; border-bottom-right-radius: 2px; } /* zoom control */ .leaflet-control-zoom-in, .leaflet-control-zoom-out { font: bold 18px 'Lucida Console', Monaco, monospace; text-indent: 1px; } .leaflet-touch .leaflet-control-zoom-in, .leaflet-touch .leaflet-control-zoom-out { font-size: 22px; } /* layers control */ .leaflet-control-layers { box-shadow: 0 1px 5px rgba(0,0,0,0.4); background: #fff; border-radius: 5px; } .leaflet-control-layers-toggle { background-image: url(images/layers.png); width: 36px; height: 36px; } .leaflet-retina .leaflet-control-layers-toggle { background-image: url(images/layers-2x.png); background-size: 26px 26px; } .leaflet-touch .leaflet-control-layers-toggle { width: 44px; height: 44px; } .leaflet-control-layers .leaflet-control-layers-list, .leaflet-control-layers-expanded .leaflet-control-layers-toggle { display: none; } .leaflet-control-layers-expanded .leaflet-control-layers-list { display: block; position: relative; } .leaflet-control-layers-expanded { padding: 6px 10px 6px 6px; color: #333; background: #fff; } .leaflet-control-layers-scrollbar { overflow-y: scroll; overflow-x: hidden; padding-right: 5px; } .leaflet-control-layers-selector { margin-top: 2px; position: relative; top: 1px; } .leaflet-control-layers label { display: block; } .leaflet-control-layers-separator { height: 0; border-top: 1px solid #ddd; margin: 5px -10px 5px -6px; } /* Default icon URLs */ .leaflet-default-icon-path { background-image: url(images/marker-icon.png); } /* attribution and scale controls */ .leaflet-container .leaflet-control-attribution { background: #fff; background: rgba(255, 255, 255, 0.7); margin: 0; } .leaflet-control-attribution, .leaflet-control-scale-line { padding: 0 5px; color: #333; } .leaflet-control-attribution a { text-decoration: none; } .leaflet-control-attribution a:hover { text-decoration: underline; } .leaflet-container .leaflet-control-attribution, .leaflet-container .leaflet-control-scale { font-size: 11px; } .leaflet-left .leaflet-control-scale { margin-left: 5px; } .leaflet-bottom .leaflet-control-scale { margin-bottom: 5px; } .leaflet-control-scale-line { border: 2px solid #777; border-top: none; line-height: 1.1; padding: 2px 5px 1px; font-size: 11px; white-space: nowrap; overflow: hidden; -moz-box-sizing: border-box; box-sizing: border-box; background: #fff; background: rgba(255, 255, 255, 0.5); } .leaflet-control-scale-line:not(:first-child) { border-top: 2px solid #777; border-bottom: none; margin-top: -2px; } .leaflet-control-scale-line:not(:first-child):not(:last-child) { border-bottom: 2px solid #777; } .leaflet-touch .leaflet-control-attribution, .leaflet-touch .leaflet-control-layers, .leaflet-touch .leaflet-bar { box-shadow: none; } .leaflet-touch .leaflet-control-layers, .leaflet-touch .leaflet-bar { border: 2px solid rgba(0,0,0,0.2); background-clip: padding-box; } /* popup */ .leaflet-popup { position: absolute; text-align: center; margin-bottom: 20px; } .leaflet-popup-content-wrapper { padding: 1px; text-align: left; border-radius: 12px; } .leaflet-popup-content { margin: 13px 19px; line-height: 1.4; } .leaflet-popup-content p { margin: 18px 0; } .leaflet-popup-tip-container { width: 40px; height: 20px; position: absolute; left: 50%; margin-left: -20px; overflow: hidden; pointer-events: none; } .leaflet-popup-tip { width: 17px; height: 17px; padding: 1px; margin: -10px auto 0; -webkit-transform: rotate(45deg); -moz-transform: rotate(45deg); -ms-transform: rotate(45deg); transform: rotate(45deg); } .leaflet-popup-content-wrapper, .leaflet-popup-tip { background: white; color: #333; box-shadow: 0 3px 14px rgba(0,0,0,0.4); } .leaflet-container a.leaflet-popup-close-button { position: absolute; top: 0; right: 0; padding: 4px 4px 0 0; border: none; text-align: center; width: 18px; height: 14px; font: 16px/14px Tahoma, Verdana, sans-serif; color: #c3c3c3; text-decoration: none; font-weight: bold; background: transparent; } .leaflet-container a.leaflet-popup-close-button:hover { color: #999; } .leaflet-popup-scrolled { overflow: auto; border-bottom: 1px solid #ddd; border-top: 1px solid #ddd; } .leaflet-oldie .leaflet-popup-content-wrapper { zoom: 1; } .leaflet-oldie .leaflet-popup-tip { width: 24px; margin: 0 auto; -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(M11=0.70710678, M12=0.70710678, M21=-0.70710678, M22=0.70710678)"; filter: progid:DXImageTransform.Microsoft.Matrix(M11=0.70710678, M12=0.70710678, M21=-0.70710678, M22=0.70710678); } .leaflet-oldie .leaflet-popup-tip-container { margin-top: -1px; } .leaflet-oldie .leaflet-control-zoom, .leaflet-oldie .leaflet-control-layers, .leaflet-oldie .leaflet-popup-content-wrapper, .leaflet-oldie .leaflet-popup-tip { border: 1px solid #999; } /* div icon */ .leaflet-div-icon { background: #fff; border: 1px solid #666; } /* Tooltip */ /* Base styles for the element that has a tooltip */ .leaflet-tooltip { position: absolute; padding: 6px; background-color: #fff; border: 1px solid #fff; border-radius: 3px; color: #222; white-space: nowrap; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; pointer-events: none; box-shadow: 0 1px 3px rgba(0,0,0,0.4); } .leaflet-tooltip.leaflet-clickable { cursor: pointer; pointer-events: auto; } .leaflet-tooltip-top:before, .leaflet-tooltip-bottom:before, .leaflet-tooltip-left:before, .leaflet-tooltip-right:before { position: absolute; pointer-events: none; border: 6px solid transparent; background: transparent; content: ""; } /* Directions */ .leaflet-tooltip-bottom { margin-top: 6px; } .leaflet-tooltip-top { margin-top: -6px; } .leaflet-tooltip-bottom:before, .leaflet-tooltip-top:before { left: 50%; margin-left: -6px; } .leaflet-tooltip-top:before { bottom: 0; margin-bottom: -12px; border-top-color: #fff; } .leaflet-tooltip-bottom:before { top: 0; margin-top: -12px; margin-left: -6px; border-bottom-color: #fff; } .leaflet-tooltip-left { margin-left: -6px; } .leaflet-tooltip-right { margin-left: 6px; } .leaflet-tooltip-left:before, .leaflet-tooltip-right:before { top: 50%; margin-top: -6px; } .leaflet-tooltip-left:before { right: 0; margin-right: -12px; border-left-color: #fff; } .leaflet-tooltip-right:before { left: 0; margin-left: -12px; border-right-color: #fff; } /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } table { border-collapse: collapse; border-spacing: 0; } html, body { width : 100%; height : 100%; } .map-container { width : 100%; height : 100%; } <link href="https://unpkg.com/leaflet#1.3.4/dist/leaflet.css" rel="stylesheet"/> <script src="https://unpkg.com/leaflet#1.3.4/dist/leaflet.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://unpkg.com/leaflet.gridlayer.googlemutant#latest/Leaflet.GoogleMutant.js"></script> <div class="map-container" id="mapid"></div> <ul class="plot"> <li class="markers" data-lattitude="-40.99497" data-longitude="174.50808">Text 1</li> <li class="markers" data-lattitude="-41.30269" data-longitude="173.63696">Text 2</li> <li class="markers" data-lattitude="-41.49413" data-longitude="173.5421">Text 3</li> <li class="markers" data-lattitude="-40.98585" data-longitude="174.50659">Text 4</li> <li class="markers" data-lattitude="-40.93163" data-longitude="173.81726">Text 5</li> </ul>
The problem is in this line: plot.push( $(this).text(), $(this).data('lattitude'), $(this).data('longitude') ); You're pushing three items to the plot array, resulting in the plot array looking something like this: ["Text 1", -40.99497, 174.50808, "Text 2", -41.30269, 173.63696 Later, when you try to iterate over the array: for (var i = 0; i < plot.length; i++) { marker = new L.marker([plot[i][1], plot[i][2]]).bindPopup(plot[i][0], { You're accessing the second and third index of the string (the first element in plot), not the second and third index of an array (resulting in the e and x you see, which came from text). Instead, push an array of those three items to plot: plot.push([ $(this).text(), $(this).data('lattitude'), $(this).data('longitude') ]); var map = L.map('mapid', { fullscreenControl: { pseudoFullscreen: false // if true, fullscreen to page width and height }, }).setView([30.539255791073, 58.383450508118], 5); map.createPane('labels'); map.getPane('labels').style.zIndex = 650; map.getPane('labels').style.pointerEvents = 'none'; var positron = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { noWrap: true, maxZoom: 10 }).addTo(map); var positronLabels = L.tileLayer('http://{s}.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}.png', { pane: 'labels', noWrap: true, maxZoom: 3 }).addTo(map); var southWest = L.latLng(-89.98155760646617, -180), northEast = L.latLng(89.99346179538875, 180), bounds = L.latLngBounds(southWest, northEast); map.setMaxBounds(bounds); map.on('drag', function() { map.panInsideBounds(bounds, { animate: false }); }); var plot = []; $('.plot > .markers').each(function() { plot.push([$(this).text(), $(this).data('lattitude'), $(this).data('longitude')]); }); console.log(plot); for (var i = 0; i < plot.length; i++) { marker = new L.marker([plot[i][1], plot[i][2]]).bindPopup(plot[i][0], { autoClose: false }).addTo(map).openPopup(); } /* required styles */ .leaflet-pane, .leaflet-tile, .leaflet-marker-icon, .leaflet-marker-shadow, .leaflet-tile-container, .leaflet-pane>svg, .leaflet-pane>canvas, .leaflet-zoom-box, .leaflet-image-layer, .leaflet-layer { position: absolute; left: 0; top: 0; } .leaflet-container { overflow: hidden; } .leaflet-tile, .leaflet-marker-icon, .leaflet-marker-shadow { -webkit-user-select: none; -moz-user-select: none; user-select: none; -webkit-user-drag: none; } /* Safari renders non-retina tile on retina better with this, but Chrome is worse */ .leaflet-safari .leaflet-tile { image-rendering: -webkit-optimize-contrast; } /* hack that prevents hw layers "stretching" when loading new tiles */ .leaflet-safari .leaflet-tile-container { width: 1600px; height: 1600px; -webkit-transform-origin: 0 0; } .leaflet-marker-icon, .leaflet-marker-shadow { display: block; } /* .leaflet-container svg: reset svg max-width decleration shipped in Joomla! (joomla.org) 3.x */ /* .leaflet-container img: map is broken in FF if you have max-width: 100% on tiles */ .leaflet-container .leaflet-overlay-pane svg, .leaflet-container .leaflet-marker-pane img, .leaflet-container .leaflet-shadow-pane img, .leaflet-container .leaflet-tile-pane img, .leaflet-container img.leaflet-image-layer, .leaflet-container .leaflet-tile { max-width: none !important; max-height: none !important; } .leaflet-container.leaflet-touch-zoom { -ms-touch-action: pan-x pan-y; touch-action: pan-x pan-y; } .leaflet-container.leaflet-touch-drag { -ms-touch-action: pinch-zoom; /* Fallback for FF which doesn't support pinch-zoom */ touch-action: none; touch-action: pinch-zoom; } .leaflet-container.leaflet-touch-drag.leaflet-touch-zoom { -ms-touch-action: none; touch-action: none; } .leaflet-container { -webkit-tap-highlight-color: transparent; } .leaflet-container a { -webkit-tap-highlight-color: rgba(51, 181, 229, 0.4); } .leaflet-tile { filter: inherit; visibility: hidden; } .leaflet-tile-loaded { visibility: inherit; } .leaflet-zoom-box { width: 0; height: 0; -moz-box-sizing: border-box; box-sizing: border-box; z-index: 800; } /* workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=888319 */ .leaflet-overlay-pane svg { -moz-user-select: none; } .leaflet-pane { z-index: 400; } .leaflet-tile-pane { z-index: 200; } .leaflet-overlay-pane { z-index: 400; } .leaflet-shadow-pane { z-index: 500; } .leaflet-marker-pane { z-index: 600; } .leaflet-tooltip-pane { z-index: 650; } .leaflet-popup-pane { z-index: 700; } .leaflet-map-pane canvas { z-index: 100; } .leaflet-map-pane svg { z-index: 200; } .leaflet-vml-shape { width: 1px; height: 1px; } .lvml { behavior: url(#default#VML); display: inline-block; position: absolute; } /* control positioning */ .leaflet-control { position: relative; z-index: 800; pointer-events: visiblePainted; /* IE 9-10 doesn't have auto */ pointer-events: auto; } .leaflet-top, .leaflet-bottom { position: absolute; z-index: 1000; pointer-events: none; } .leaflet-top { top: 0; } .leaflet-right { right: 0; } .leaflet-bottom { bottom: 0; } .leaflet-left { left: 0; } .leaflet-control { float: left; clear: both; } .leaflet-right .leaflet-control { float: right; } .leaflet-top .leaflet-control { margin-top: 10px; } .leaflet-bottom .leaflet-control { margin-bottom: 10px; } .leaflet-left .leaflet-control { margin-left: 10px; } .leaflet-right .leaflet-control { margin-right: 10px; } /* zoom and fade animations */ .leaflet-fade-anim .leaflet-tile { will-change: opacity; } .leaflet-fade-anim .leaflet-popup { opacity: 0; -webkit-transition: opacity 0.2s linear; -moz-transition: opacity 0.2s linear; transition: opacity 0.2s linear; } .leaflet-fade-anim .leaflet-map-pane .leaflet-popup { opacity: 1; } .leaflet-zoom-animated { -webkit-transform-origin: 0 0; -ms-transform-origin: 0 0; transform-origin: 0 0; } .leaflet-zoom-anim .leaflet-zoom-animated { will-change: transform; } .leaflet-zoom-anim .leaflet-zoom-animated { -webkit-transition: -webkit-transform 0.25s cubic-bezier(0, 0, 0.25, 1); -moz-transition: -moz-transform 0.25s cubic-bezier(0, 0, 0.25, 1); transition: transform 0.25s cubic-bezier(0, 0, 0.25, 1); } .leaflet-zoom-anim .leaflet-tile, .leaflet-pan-anim .leaflet-tile { -webkit-transition: none; -moz-transition: none; transition: none; } .leaflet-zoom-anim .leaflet-zoom-hide { visibility: hidden; } /* cursors */ .leaflet-interactive { cursor: pointer; } .leaflet-grab { cursor: -webkit-grab; cursor: -moz-grab; cursor: grab; } .leaflet-crosshair, .leaflet-crosshair .leaflet-interactive { cursor: crosshair; } .leaflet-popup-pane, .leaflet-control { cursor: auto; } .leaflet-dragging .leaflet-grab, .leaflet-dragging .leaflet-grab .leaflet-interactive, .leaflet-dragging .leaflet-marker-draggable { cursor: move; cursor: -webkit-grabbing; cursor: -moz-grabbing; cursor: grabbing; } /* marker & overlays interactivity */ .leaflet-marker-icon, .leaflet-marker-shadow, .leaflet-image-layer, .leaflet-pane>svg path, .leaflet-tile-container { pointer-events: none; } .leaflet-marker-icon.leaflet-interactive, .leaflet-image-layer.leaflet-interactive, .leaflet-pane>svg path.leaflet-interactive { pointer-events: visiblePainted; /* IE 9-10 doesn't have auto */ pointer-events: auto; } /* visual tweaks */ .leaflet-container { background: #ddd; outline: 0; } .leaflet-container a { color: #0078A8; } .leaflet-container a.leaflet-active { outline: 2px solid orange; } .leaflet-zoom-box { border: 2px dotted #38f; background: rgba(255, 255, 255, 0.5); } /* general typography */ .leaflet-container { font: 12px/1.5 "Helvetica Neue", Arial, Helvetica, sans-serif; } /* general toolbar styles */ .leaflet-bar { box-shadow: 0 1px 5px rgba(0, 0, 0, 0.65); border-radius: 4px; } .leaflet-bar a, .leaflet-bar a:hover { background-color: #fff; border-bottom: 1px solid #ccc; width: 26px; height: 26px; line-height: 26px; display: block; text-align: center; text-decoration: none; color: black; } .leaflet-bar a, .leaflet-control-layers-toggle { background-position: 50% 50%; background-repeat: no-repeat; display: block; } .leaflet-bar a:hover { background-color: #f4f4f4; } .leaflet-bar a:first-child { border-top-left-radius: 4px; border-top-right-radius: 4px; } .leaflet-bar a:last-child { border-bottom-left-radius: 4px; border-bottom-right-radius: 4px; border-bottom: none; } .leaflet-bar a.leaflet-disabled { cursor: default; background-color: #f4f4f4; color: #bbb; } .leaflet-touch .leaflet-bar a { width: 30px; height: 30px; line-height: 30px; } .leaflet-touch .leaflet-bar a:first-child { border-top-left-radius: 2px; border-top-right-radius: 2px; } .leaflet-touch .leaflet-bar a:last-child { border-bottom-left-radius: 2px; border-bottom-right-radius: 2px; } /* zoom control */ .leaflet-control-zoom-in, .leaflet-control-zoom-out { font: bold 18px 'Lucida Console', Monaco, monospace; text-indent: 1px; } .leaflet-touch .leaflet-control-zoom-in, .leaflet-touch .leaflet-control-zoom-out { font-size: 22px; } /* layers control */ .leaflet-control-layers { box-shadow: 0 1px 5px rgba(0, 0, 0, 0.4); background: #fff; border-radius: 5px; } .leaflet-control-layers-toggle { background-image: url(images/layers.png); width: 36px; height: 36px; } .leaflet-retina .leaflet-control-layers-toggle { background-image: url(images/layers-2x.png); background-size: 26px 26px; } .leaflet-touch .leaflet-control-layers-toggle { width: 44px; height: 44px; } .leaflet-control-layers .leaflet-control-layers-list, .leaflet-control-layers-expanded .leaflet-control-layers-toggle { display: none; } .leaflet-control-layers-expanded .leaflet-control-layers-list { display: block; position: relative; } .leaflet-control-layers-expanded { padding: 6px 10px 6px 6px; color: #333; background: #fff; } .leaflet-control-layers-scrollbar { overflow-y: scroll; overflow-x: hidden; padding-right: 5px; } .leaflet-control-layers-selector { margin-top: 2px; position: relative; top: 1px; } .leaflet-control-layers label { display: block; } .leaflet-control-layers-separator { height: 0; border-top: 1px solid #ddd; margin: 5px -10px 5px -6px; } /* Default icon URLs */ .leaflet-default-icon-path { background-image: url(images/marker-icon.png); } /* attribution and scale controls */ .leaflet-container .leaflet-control-attribution { background: #fff; background: rgba(255, 255, 255, 0.7); margin: 0; } .leaflet-control-attribution, .leaflet-control-scale-line { padding: 0 5px; color: #333; } .leaflet-control-attribution a { text-decoration: none; } .leaflet-control-attribution a:hover { text-decoration: underline; } .leaflet-container .leaflet-control-attribution, .leaflet-container .leaflet-control-scale { font-size: 11px; } .leaflet-left .leaflet-control-scale { margin-left: 5px; } .leaflet-bottom .leaflet-control-scale { margin-bottom: 5px; } .leaflet-control-scale-line { border: 2px solid #777; border-top: none; line-height: 1.1; padding: 2px 5px 1px; font-size: 11px; white-space: nowrap; overflow: hidden; -moz-box-sizing: border-box; box-sizing: border-box; background: #fff; background: rgba(255, 255, 255, 0.5); } .leaflet-control-scale-line:not(:first-child) { border-top: 2px solid #777; border-bottom: none; margin-top: -2px; } .leaflet-control-scale-line:not(:first-child):not(:last-child) { border-bottom: 2px solid #777; } .leaflet-touch .leaflet-control-attribution, .leaflet-touch .leaflet-control-layers, .leaflet-touch .leaflet-bar { box-shadow: none; } .leaflet-touch .leaflet-control-layers, .leaflet-touch .leaflet-bar { border: 2px solid rgba(0, 0, 0, 0.2); background-clip: padding-box; } /* popup */ .leaflet-popup { position: absolute; text-align: center; margin-bottom: 20px; } .leaflet-popup-content-wrapper { padding: 1px; text-align: left; border-radius: 12px; } .leaflet-popup-content { margin: 13px 19px; line-height: 1.4; } .leaflet-popup-content p { margin: 18px 0; } .leaflet-popup-tip-container { width: 40px; height: 20px; position: absolute; left: 50%; margin-left: -20px; overflow: hidden; pointer-events: none; } .leaflet-popup-tip { width: 17px; height: 17px; padding: 1px; margin: -10px auto 0; -webkit-transform: rotate(45deg); -moz-transform: rotate(45deg); -ms-transform: rotate(45deg); transform: rotate(45deg); } .leaflet-popup-content-wrapper, .leaflet-popup-tip { background: white; color: #333; box-shadow: 0 3px 14px rgba(0, 0, 0, 0.4); } .leaflet-container a.leaflet-popup-close-button { position: absolute; top: 0; right: 0; padding: 4px 4px 0 0; border: none; text-align: center; width: 18px; height: 14px; font: 16px/14px Tahoma, Verdana, sans-serif; color: #c3c3c3; text-decoration: none; font-weight: bold; background: transparent; } .leaflet-container a.leaflet-popup-close-button:hover { color: #999; } .leaflet-popup-scrolled { overflow: auto; border-bottom: 1px solid #ddd; border-top: 1px solid #ddd; } .leaflet-oldie .leaflet-popup-content-wrapper { zoom: 1; } .leaflet-oldie .leaflet-popup-tip { width: 24px; margin: 0 auto; -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(M11=0.70710678, M12=0.70710678, M21=-0.70710678, M22=0.70710678)"; filter: progid:DXImageTransform.Microsoft.Matrix(M11=0.70710678, M12=0.70710678, M21=-0.70710678, M22=0.70710678); } .leaflet-oldie .leaflet-popup-tip-container { margin-top: -1px; } .leaflet-oldie .leaflet-control-zoom, .leaflet-oldie .leaflet-control-layers, .leaflet-oldie .leaflet-popup-content-wrapper, .leaflet-oldie .leaflet-popup-tip { border: 1px solid #999; } /* div icon */ .leaflet-div-icon { background: #fff; border: 1px solid #666; } /* Tooltip */ /* Base styles for the element that has a tooltip */ .leaflet-tooltip { position: absolute; padding: 6px; background-color: #fff; border: 1px solid #fff; border-radius: 3px; color: #222; white-space: nowrap; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; pointer-events: none; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4); } .leaflet-tooltip.leaflet-clickable { cursor: pointer; pointer-events: auto; } .leaflet-tooltip-top:before, .leaflet-tooltip-bottom:before, .leaflet-tooltip-left:before, .leaflet-tooltip-right:before { position: absolute; pointer-events: none; border: 6px solid transparent; background: transparent; content: ""; } /* Directions */ .leaflet-tooltip-bottom { margin-top: 6px; } .leaflet-tooltip-top { margin-top: -6px; } .leaflet-tooltip-bottom:before, .leaflet-tooltip-top:before { left: 50%; margin-left: -6px; } .leaflet-tooltip-top:before { bottom: 0; margin-bottom: -12px; border-top-color: #fff; } .leaflet-tooltip-bottom:before { top: 0; margin-top: -12px; margin-left: -6px; border-bottom-color: #fff; } .leaflet-tooltip-left { margin-left: -6px; } .leaflet-tooltip-right { margin-left: 6px; } .leaflet-tooltip-left:before, .leaflet-tooltip-right:before { top: 50%; margin-top: -6px; } .leaflet-tooltip-left:before { right: 0; margin-right: -12px; border-left-color: #fff; } .leaflet-tooltip-right:before { left: 0; margin-left: -12px; border-right-color: #fff; } /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } table { border-collapse: collapse; border-spacing: 0; } html, body { width: 100%; height: 100%; } .map-container { width: 100%; height: 100%; } <link href="https://unpkg.com/leaflet#1.3.4/dist/leaflet.css" rel="stylesheet" /> <script src="https://unpkg.com/leaflet#1.3.4/dist/leaflet.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://unpkg.com/leaflet.gridlayer.googlemutant#latest/Leaflet.GoogleMutant.js"></script> <div class="map-container" id="mapid"></div> <ul class="plot"> <li class="markers" data-lattitude="-40.99497" data-longitude="174.50808">Text 1</li> <li class="markers" data-lattitude="-41.30269" data-longitude="173.63696">Text 2</li> <li class="markers" data-lattitude="-41.49413" data-longitude="173.5421">Text 3</li> <li class="markers" data-lattitude="-40.98585" data-longitude="174.50659">Text 4</li> <li class="markers" data-lattitude="-40.93163" data-longitude="173.81726">Text 5</li> </ul>
How to change color of icons on each custom range steps
I have a custom range I have styled and modified. For each step there is a text value displayed underneath the range in a green box, and icons at the top of each step. I would like to know, how can I change the background color of the icons when a step has been selected? I have added some additional javascript code that is not working properly. For instance, on the first step, the group icon will be green, and when you select the next step the person will be green and the group will change to default grey, and so on and so forth... Thank you. var arr = new Array(); arr[1] = "everyone"; arr[2] = "show my group only"; arr[3] = "show only me"; var rangeSlider = function() { var slider = $(".range-slider"), range = $(".range-slider__range"), value = $(".range-slider__value"); slider.each(function() { value.each(function() { var value = $(this) .prev() .attr("value"); $(this).html(arr[value]); }); range.on("input", function() { $(this) .next(value) .html(arr[this.value]); }); // Set active icons $('.range-icons li').removeClass('active selected'); var icons = $('.range-icons').find('li:nth-child(' + icons + ')'); icons.addClass('active selected'); return style; }); }; rangeSlider(); *, *:before, *:after { box-sizing: border-box; } body { font-family: sans-serif; padding: 60px 20px; } #media (min-width: 600px) { body { padding: 60px; } } .range-slider { margin: 0; } .range-slider { width: 24%; } .range-slider__range { -webkit-appearance: none; /*width: calc(100% - (73px));*/ width: 100%; height: 6px; border-radius: 5px; background: #d7dcdf; outline: none; padding: 0; margin: 0; } .range-slider__range::-webkit-slider-thumb { appearance: none; width: 18px; height: 18px; border-radius: 50%; background: #2c3e50; cursor: pointer; transition: background .15s ease-in-out; } .range-slider__range::-webkit-slider-thumb:hover { background: #1abc9c; } .range-slider__range:active::-webkit-slider-thumb { background: #1abc9c; } .range-slider__range::-moz-range-thumb { width: 18px; height: 18px; border: 0; border-radius: 50%; background: #2c3e50; cursor: grab; transition: background .15s ease-in-out; } .range-slider__range:active::-moz-range-thumb { cursor: grabbing; background: #1abc9c; } .range-slider__range::-moz-range-thumb:hover { background: #1abc9c; } .range-slider__value { display: block; position: relative; color: #fff; font-size: 12px; margin-top: 10px; line-height: 20px; text-align: center; background: green; padding: 0; } /*.range-slider__value:after { position: absolute; top: 8px; left: -7px; width: 0; height: 0; border-top: 7px solid transparent; border-right: 7px solid #2c3e50; border-bottom: 7px solid transparent; content: ''; }*/ ::-moz-range-track { background: #d7dcdf; border: 0; } input::-moz-focus-inner, input::-moz-focus-outer { border: 0; } /*.range-labels { margin: 9px -21px 0; padding: 0; list-style: none; } .range-labels li { position: relative; float: left; width: 60px; text-align: center; color: #b2b2b2; font-size: 14px; cursor: pointer; } .range-labels .active { color: #37adbf; } .range-labels .selected::before { background: #37adbf; } .range-labels .active.selected::before { display: none; }*/ /*icons*/ .range-icons { margin: 9px -20px 0; padding: 0; list-style: none; } .range-icons li { position: relative; float: left; width: 33%; text-align: center; color: #b2b2b2; font-size: 10px; } .range-icons .active { color: #37adbf; } .range-icons .selected::before { background: #37adbf; } .range-icons .active.selected::before { display: none; } <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <div class="range-slider"> <ul class="range-icons clearfix"> <li class="active selected"><i class="material-icons">group</i></li> <li><i class="material-icons">person</i></li> <li><i class="material-icons">lock</i></li> </ul> <input class="range-slider__range" type="range" value="1" min="1" max="3" step="1"> <span class="range-slider__value">0</span> </div>
you can create classes and add the apropriate class with addClass whenever the the range input changes in range.on("input", with $('.material-icons:nth('+ ( this.value - 1) +')').addClass('class-'+(this.value)) since your this.value starts from 1 : var arr = new Array(); arr[1] = "everyone"; arr[2] = "show my group only"; arr[3] = "show only me"; var rangeSlider = function() { var slider = $(".range-slider"), range = $(".range-slider__range"), value = $(".range-slider__value"); slider.each(function() { value.each(function() { var value = $(this) .prev() .attr("value"); $(this).html(arr[value]); }); range.on("input", function() { $(this) .next(value) .html(arr[this.value]); $('.material-icons').attr('class', 'material-icons') $('.material-icons:nth('+ ( this.value - 1) +')').addClass('material-icons class-'+(this.value)) }); }); }; rangeSlider(); *, *:before, *:after { box-sizing: border-box; } body { font-family: sans-serif; padding: 60px 20px; } #media (min-width: 600px) { body { padding: 60px; } } .range-slider { margin: 0; } .range-slider { width: 24%; } .range-slider__range { -webkit-appearance: none; /*width: calc(100% - (73px));*/ width: 100%; height: 6px; border-radius: 5px; background: #d7dcdf; outline: none; padding: 0; margin: 0; } .range-slider__range::-webkit-slider-thumb { appearance: none; width: 18px; height: 18px; border-radius: 50%; background: #2c3e50; cursor: pointer; transition: background .15s ease-in-out; } .range-slider__range::-webkit-slider-thumb:hover { background: #1abc9c; } .range-slider__range:active::-webkit-slider-thumb { background: #1abc9c; } .range-slider__range::-moz-range-thumb { width: 18px; height: 18px; border: 0; border-radius: 50%; background: #2c3e50; cursor: grab; transition: background .15s ease-in-out; } .range-slider__range:active::-moz-range-thumb { cursor: grabbing; background: #1abc9c; } .range-slider__range::-moz-range-thumb:hover { background: #1abc9c; } .range-slider__value { display: block; position: relative; color: #fff; font-size: 12px; margin-top: 10px; line-height: 20px; text-align: center; background: green; padding: 0; } /*.range-slider__value:after { position: absolute; top: 8px; left: -7px; width: 0; height: 0; border-top: 7px solid transparent; border-right: 7px solid #2c3e50; border-bottom: 7px solid transparent; content: ''; }*/ ::-moz-range-track { background: #d7dcdf; border: 0; } input::-moz-focus-inner, input::-moz-focus-outer { border: 0; } /*.range-labels { margin: 9px -21px 0; padding: 0; list-style: none; } .range-labels li { position: relative; float: left; width: 60px; text-align: center; color: #b2b2b2; font-size: 14px; cursor: pointer; } .range-labels .active { color: #37adbf; } .range-labels .selected::before { background: #37adbf; } .range-labels .active.selected::before { display: none; }*/ /*icons*/ .range-icons { margin: 9px -20px 0; padding: 0; list-style: none; } .range-icons li { position: relative; float: left; width: 33%; text-align: center; color: #b2b2b2; font-size: 10px; } /* classes with colors you want */ .class-1{ color: red; } .class-2{ color: blue; } .class-3{ color: orange; } <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <div class="range-slider"> <ul class="range-icons clearfix"> <li class="active selected"><i class="material-icons class-1">group</i></li> <li><i class="material-icons">person</i></li> <li><i class="material-icons">lock</i></li> </ul> <input class="range-slider__range" type="range" value="1" min="1" max="3" step="1"> <span class="range-slider__value">0</span> </div>
One methos I use is using svg icons and implement the raw code. You can then manipulate this source with clases and style settings. Look inside the svg source and finmd the path and rect tags. document.getElementById("ranger").onchange = function(event) { document.querySelector(".symbol.standard").style.fill = "rgb(" + event.target.value + ", 80, 160)"; } svg { width:200px; .symbol.standard { fill: #f00; } .symbol.other { fill: rgb(80, 80, 160); } <input id="ranger" type='range' min="0" max="255"> <svg class="singleseat " xmlns="http://www.w3.org/2000/svg" viewBox="0 0 90.3 63.3"> <title>seat</title> <rect class="symbol standard" x="16.7" width="57" height="49" rx="12" ry="12"></rect> <path class="symbol other" d="M79.9,15.8V42.3a12,12,0,0,1-12,12H22.5a12,12,0,0,1-12-12V15.8H0V51.3a12,12,0,0,0,12,12H78.3a12,12,0,0,0,12-12V15.8Z"></path> </svg>
Getting JSON from URL and then display results in accordion using Javascript
I have created an accordion which I would like to populate using JSON from this link : http://design.propcom.co.uk/buildtest/accordion-data.json Here is my html: <div class="accordion js-accordion"> <div class="accordion__item js-accordion-item active"> <div class="accordion-header js-accordion-header"></div> <div class="accordion-body js-accordion-body" > <div class="accordion-body__contents" ></div> </div><!-- end of accordion body --> </div><!-- end of accordion item --> <div class="accordion__item js-accordion-item "> <div class="accordion-header js-accordion-header"></div> <div class="accordion-body js-accordion-body"> <div class="accordion-body__contents"></div> </div> </div> <div class="accordion__item js-accordion-item"> <div class="accordion-header js-accordion-header"></div> <div class="accordion-body js-accordion-body"> <div class="accordion-body__contents"></div> </div> </div> </div><!-- end of accordion --> I am trying to populate the accordion-header js-accordion-header div with the "heading" data from the JSON file and accordion-body__contents with the "contents" data. This is as far as I have got with the Javascript part: $.ajax({ url: 'http://design.propcom.co.uk/buildtest/accordion-data.json', type: 'GET', dataType: 'JSON', success: function (data) { $.each(data.blocks, function(index, element) { $(".accordion-header").append("<div>" + element.heading + "</div>"); $(".accordion-body__contents").append("<div>" + element.content + "</div>"); }); } }); Any help would be greatly appreciated as I feel like I've hit a wall with my current efforts.
There are a few issues here: We're a bit off when handling the AJAX results Use the data.blocks property and while looping use the element variable. As #Danny suggested we have extra AJAX properties Remove the callback and data properties. Lastly your html seems over complicated After seeing the css from your demo site, I was able to include it and remove the JQueryUI thought. Now be sure to append the entire accordion__item element. With these problems ironed out it should look similar to this: $.ajax({ url: 'https://design.propcom.co.uk/buildtest/accordion-data.json', type: 'GET', dataType: 'JSON', success: function (data) { $.each(data.blocks, function(index, element) { $(".accordion") .append(`<div class="accordion__item js-accordion-item "> <div class="accordion-header js-accordion-header">${element.heading}</div> <div class="accordion-body js-accordion-body"> <div class="accordion-body__contents">${element.content}</div> </div> </div>`); }); var accordion = (function(){ var $accordion = $('.js-accordion'); var $accordion_header = $accordion.find('.js-accordion-header'); var $accordion_item = $('.js-accordion-item'); // default settings var settings = { // animation speed speed: 400, // close all other accordion items if true oneOpen: false }; return { // pass configurable object literal init: function($settings) { $accordion_header.on('click', function() { accordion.toggle($(this)); }); $.extend(settings, $settings); // ensure only one accordion is active if oneOpen is true if(settings.oneOpen && $('.js-accordion-item.active').length > 1) { $('.js-accordion-item.active:not(:first)').removeClass('active'); } // reveal the active accordion bodies $('.js-accordion-item.active').find('> .js-accordion-body').show(); }, toggle: function($this) { if(settings.oneOpen && $this[0] != $this.closest('.js-accordion').find('> .js-accordion-item.active > .js-accordion-header')[0]) { $this.closest('.js-accordion') .find('> .js-accordion-item') .removeClass('active') .find('.js-accordion-body') .slideUp() } // show/hide the clicked accordion item $this.closest('.js-accordion-item').toggleClass('active'); $this.next().stop().slideToggle(settings.speed); } } })(); $(document).ready(function(){accordion.init({ speed: 300, oneOpen: true });}); } }); body { font-size: 62.5%; background: #ffffff; font-family: 'Open Sans', sans-serif; line-height: 2; padding: 5em; } .accordion { font-size: 1rem; width: 30vw; margin: 0 auto; border-radius: 5px; } .accordion-header, .accordion-body { background: white; } .accordion-header { padding: 1.5em 1.5em; margin-bottom:6px; box-shadow: 0px 4px #6F277D; background: #9E38B0; text-transform: uppercase; color: white; cursor: pointer; font-size: .8em; letter-spacing: .1em; transition: all .3s; } .accordion-header:hover { background: #6844B7; box-shadow: 0px 4px #4C3185; position: relative; z-index: 5; } .accordion-body { background: #fcfcfc; color: #3f3c3c; display: none; } .accordion-body__contents { padding: 1.5em 1.5em; font-size: .85em; } .accordion__item.active:last-child .accordion-header { border-radius: none; } .accordion:first-child > .accordion__item > .accordion-header { border-bottom: 1px solid transparent; } .accordion__item > .accordion-header:after { content: "\f3d0"; font-family: IonIcons; font-size: 1.2em; float: right; position: relative; top: -2px; transition: .3s all; transform: rotate(0deg); } .accordion__item.active > .accordion-header:after { transform: rotate(-180deg); } .accordion__item.active .accordion-header { background: #6844B7; box-shadow: 0px 4px #4C3185; } .accordion__item .accordion__item .accordion-header { background: #f1f1f1; color: black; } #media screen and (max-width: 1000px) { body { padding: 1em; } .accordion { width: 100%; } }body { font-size: 62.5%; background: #ffffff; font-family: 'Open Sans', sans-serif; line-height: 2; padding: 5em; } .accordion { font-size: 1rem; width: 30vw; margin: 0 auto; border-radius: 5px; } .accordion-header, .accordion-body { background: white; } .accordion-header { padding: 1.5em 1.5em; margin-bottom:6px; box-shadow: 0px 4px #6F277D; background: #9E38B0; text-transform: uppercase; color: white; cursor: pointer; font-size: .8em; letter-spacing: .1em; transition: all .3s; } .accordion-header:hover { background: #6844B7; box-shadow: 0px 4px #4C3185; position: relative; z-index: 5; } .accordion-body { background: #fcfcfc; color: #3f3c3c; display: none; } .accordion-body__contents { padding: 1.5em 1.5em; font-size: .85em; } .accordion__item.active:last-child .accordion-header { border-radius: none; } .accordion:first-child > .accordion__item > .accordion-header { border-bottom: 1px solid transparent; } .accordion__item > .accordion-header:after { content: "\f3d0"; font-family: IonIcons; font-size: 1.2em; float: right; position: relative; top: -2px; transition: .3s all; transform: rotate(0deg); } .accordion__item.active > .accordion-header:after { transform: rotate(-180deg); } .accordion__item.active .accordion-header { background: #6844B7; box-shadow: 0px 4px #4C3185; } .accordion__item .accordion__item .accordion-header { background: #f1f1f1; color: black; } #media screen and (max-width: 1000px) { body { padding: 1em; } .accordion { width: 100%; } }body { font-size: 62.5%; background: #ffffff; font-family: 'Open Sans', sans-serif; line-height: 2; padding: 5em; } .accordion { font-size: 1rem; width: 30vw; margin: 0 auto; border-radius: 5px; } .accordion-header, .accordion-body { background: white; } .accordion-header { padding: 1.5em 1.5em; margin-bottom:6px; box-shadow: 0px 4px #6F277D; background: #9E38B0; text-transform: uppercase; color: white; cursor: pointer; font-size: .8em; letter-spacing: .1em; transition: all .3s; } .accordion-header:hover { background: #6844B7; box-shadow: 0px 4px #4C3185; position: relative; z-index: 5; } .accordion-body { background: #fcfcfc; color: #3f3c3c; display: none; } .accordion-body__contents { padding: 1.5em 1.5em; font-size: .85em; } .accordion__item.active:last-child .accordion-header { border-radius: none; } .accordion:first-child > .accordion__item > .accordion-header { border-bottom: 1px solid transparent; } .accordion__item > .accordion-header:after { content: "\f3d0"; font-family: IonIcons; font-size: 1.2em; float: right; position: relative; top: -2px; transition: .3s all; transform: rotate(0deg); } .accordion__item.active > .accordion-header:after { transform: rotate(-180deg); } .accordion__item.active .accordion-header { background: #6844B7; box-shadow: 0px 4px #4C3185; } .accordion__item .accordion__item .accordion-header { background: #f1f1f1; color: black; } #media screen and (max-width: 1000px) { body { padding: 1em; } .accordion { width: 100%; } }body { font-size: 62.5%; background: #ffffff; font-family: 'Open Sans', sans-serif; line-height: 2; padding: 5em; } .accordion { font-size: 1rem; width: 30vw; margin: 0 auto; border-radius: 5px; } .accordion-header, .accordion-body { background: white; } .accordion-header { padding: 1.5em 1.5em; margin-bottom:6px; box-shadow: 0px 4px #6F277D; background: #9E38B0; text-transform: uppercase; color: white; cursor: pointer; font-size: .8em; letter-spacing: .1em; transition: all .3s; } .accordion-header:hover { background: #6844B7; box-shadow: 0px 4px #4C3185; position: relative; z-index: 5; } .accordion-body { background: #fcfcfc; color: #3f3c3c; display: none; } .accordion-body__contents { padding: 1.5em 1.5em; font-size: .85em; } .accordion__item.active:last-child .accordion-header { border-radius: none; } .accordion:first-child > .accordion__item > .accordion-header { border-bottom: 1px solid transparent; } .accordion__item > .accordion-header:after { content: "\f3d0"; font-family: IonIcons; font-size: 1.2em; float: right; position: relative; top: -2px; transition: .3s all; transform: rotate(0deg); } .accordion__item.active > .accordion-header:after { transform: rotate(-180deg); } .accordion__item.active .accordion-header { background: #6844B7; box-shadow: 0px 4px #4C3185; } .accordion__item .accordion__item .accordion-header { background: #f1f1f1; color: black; } #media screen and (max-width: 1000px) { body { padding: 1em; } .accordion { width: 100%; } }body { font-size: 62.5%; background: #ffffff; font-family: 'Open Sans', sans-serif; line-height: 2; padding: 5em; } .accordion { font-size: 1rem; width: 30vw; margin: 0 auto; border-radius: 5px; } .accordion-header, .accordion-body { background: white; } .accordion-header { padding: 1.5em 1.5em; margin-bottom:6px; box-shadow: 0px 4px #6F277D; background: #9E38B0; text-transform: uppercase; color: white; cursor: pointer; font-size: .8em; letter-spacing: .1em; transition: all .3s; } .accordion-header:hover { background: #6844B7; box-shadow: 0px 4px #4C3185; position: relative; z-index: 5; } .accordion-body { background: #fcfcfc; color: #3f3c3c; display: none; } .accordion-body__contents { padding: 1.5em 1.5em; font-size: .85em; } .accordion__item.active:last-child .accordion-header { border-radius: none; } .accordion:first-child > .accordion__item > .accordion-header { border-bottom: 1px solid transparent; } .accordion__item > .accordion-header:after { content: "\f3d0"; font-family: IonIcons; font-size: 1.2em; float: right; position: relative; top: -2px; transition: .3s all; transform: rotate(0deg); } .accordion__item.active > .accordion-header:after { transform: rotate(-180deg); } .accordion__item.active .accordion-header { background: #6844B7; box-shadow: 0px 4px #4C3185; } .accordion__item .accordion__item .accordion-header { background: #f1f1f1; color: black; } #media screen and (max-width: 1000px) { body { padding: 1em; } .accordion { width: 100%; } }/* CSS Document */ <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="accordion js-accordion"> </div> After skimming your demo, I've included your CSS into my example.
How to put animation on thumb of input type range ::on hover?
Here, i want to put some animation on input type range thumb. here i done all the thing but i am not find the solution of animation effect on hover here is the sample example and My code.. Please give solution. Here is example that exactly what i want. #import 'bourbon'; $slider-width-number: 240; $slider-width: #{$slider-width-number}px; $slider-height: 2px; $background-slider: #c7c7c7; $background-filled-slider: #00BCD4; $thumb-width: 18px; $thumb-height: 18px; $thumb-radius: 50%; $thumb-background: #00BCD4; $thumb-box-shadow: 2px 2px 1px 10px #00BCD4; $thumb-border: 1px solid #777; $shadow-size: -8px; $fit-thumb-in-slider: -8px; #function makelongshadow($color, $size) { $val: 5px 0 0 $size $color; #for $i from 6 through $slider-width-number { $val: #{$val}, #{$i}px 0 0 $size #{$color}; } #return $val; } div { height: 100px; display: flex; justify-content: center; } input { align-items: center; appearance: none; background: none; cursor: pointer; display: flex; height: 100%; min-height: 50px; overflow: hidden; width: $slider-width; &:focus { box-shadow: none; outline: none; } &::-webkit-slider-runnable-track { background: $background-filled-slider; content: ''; height: $slider-height; pointer-events: none; } &::-webkit-slider-thumb { #include size($thumb-width $thumb-height); appearance: none; background: $thumb-background; border-radius: $thumb-radius; box-shadow: makelongshadow($background-slider, $shadow-size); margin-top: $fit-thumb-in-slider; border: $thumb-border; } &::-moz-range-track { width: $slider-width; height: $slider-height; } &::-moz-range-thumb { #include size($thumb-width $thumb-height); background: $thumb-background; border-radius: $thumb-radius; border: $thumb-border; position: relative; } &:active::-webkit-slider-thumb { transform: scale(2); } &::-moz-range-progress { height: $slider-height; background: $background-filled-slider; border: 0; margin-top: 0; } &::-ms-track { background: transparent; border: 0; border-color: transparent; border-radius: 0; border-width: 0; color: transparent; height: $slider-height; margin-top: 10px; width: $slider-width; } &::-ms-thumb { #include size($thumb-width $thumb-height); background: $thumb-background; border-radius: $thumb-radius; border: $thumb-border; } &::-ms-fill-lower { background: $background-filled-slider; border-radius: 0; } &::-ms-fill-upper { background: $background-slider; border-radius: 0; } &::-ms-tooltip { display: none; } } <div> <input type="range" min="0" max="100" value="40"/> </div> Here is the Codepen link: https://codepen.io/anon/pen/qPpRJp You can also edit Codepen
Here is what exactly you wanted. if you want more modification, go and see the documentation here https://css-tricks.com/styling-cross-browser-compatible-range-inputs-css/ /* Special styling for WebKit/Blink */ input[type=range] { -webkit-appearance: none; } input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; border: 10; height: 15px; width: 15px; border-radius: 50%; background: #3399cc; cursor: pointer; margin-top: -5px; /* You need to specify a margin in Chrome, but in Firefox and IE it is automatic */ transition : all .3s; border:0; } input[type=range]:hover::-webkit-slider-thumb{ box-shadow: 0px 0px 0px 4px rgba(51, 153, 204, 0.49); } input[type=range]::-webkit-slider-runnable-track { width: 100%; height: 4px; cursor: pointer; background: #3071a9; } <div> <input type="range" min="0" max="100" value="40"/> </div>