Вопрос по javascript, касающийся нескольких настраиваемых раскрывающихся списков на одной страницеCSS

Разбираемся в CSS
Ответить
Anonymous
 Вопрос по javascript, касающийся нескольких настраиваемых раскрывающихся списков на одной странице

Сообщение Anonymous »

Я использую сценарий Super JS для настраиваемого раскрывающегося меню, который нашел на CodePen.
Я дополнительно настроил его, но теперь пытаюсь вставить одно и то же раскрывающееся меню несколько раз на одну страницу.
Вот ссылка на PEN, где вы можете увидеть сценарий.
Я уверен, что мне нужно использовать идентификаторы, но не знаю, как правильно изменить сценарий.
Спасибо.
ССЫЛКА на Pen: https://codepen.io/pierre-laurent/pen/LVLgVw
Вот код JS:

Код: Выделить всё

$(document).ready(function() {

var countOption = $('.old-select option').length;

function openSelect() {
var heightSelect = $('.new-select').height();
var j = 1;
$('.new-select .new-option').each(function() {
$(this).addClass('reveal');
$(this).css({
'box-shadow': '0 1px 1px rgba(0,0,0,0.1)',
'left': '0',
'right': '0',
'top': j * (heightSelect + 1) + 'px'
});
j++;
});
}

function closeSelect() {
var i = 0;
$('.new-select .new-option').each(function() {
$(this).removeClass('reveal');
if (i < countOption - 3) {
$(this).css('top', 0);
$(this).css('box-shadow', 'none');
} else if (i === countOption - 3) {
$(this).css('top', '3px');
} else if (i === countOption - 2) {
$(this).css({
'top': '7px',
'left': '2px',
'right': '2px'
});
} else if (i === countOption - 1) {
$(this).css({
'top': '11px',
'left': '4px',
'right': '4px'
});
}
i++;
});
}

// Initialisation
if ($('.old-select option[selected]').length === 1) {
$('.selection p span').html($('.old-select option[selected]').html());
} else {
$('.selection p span').html($('.old-select option:first-child').html());
}

$('.old-select option').each(function() {
newValue = $(this).val();
newHTML = $(this).html();
$('.new-select').append('' + newHTML + '
');
});

var reverseIndex = countOption;
$('.new-select .new-option').each(function() {
$(this).css('z-index', reverseIndex);
reverseIndex = reverseIndex - 1;
});

closeSelect();

// Ouverture / Fermeture
$('.selection').click(function() {
$(this).toggleClass('open');
if ($(this).hasClass('open') === true) {
openSelect();
} else {
closeSelect();
}
});

// Selection
$('.new-option').click(function() {
var newValue = $(this).data('value');

// Selection New Select
$('.selection p span').html($(this).find('p').html());
$('.selection').click();

// Selection Old Select
$('.old-select option[selected]').removeAttr('selected');
$('.old-select option[value="' + newValue + '"]').attr('selected', '');

// Visuellement l'option dans le old-select ne change pas
// mais la value selectionnée est bien pris en compte lors
// de l'envoi du formulaire.  Test à l'appui.

});
});

Код: Выделить всё

/* Design tiré du site flatuicolors.com */

/* Réinitialisation */

*{
margin: 0; padding: 0;
box-sizing:         border-box;
-moz-box-sizing:    border-box;
-webkit-box-sizing: border-box;
}

body{font-family: 'Open Sans';}
a{color: #000; text-decoration: none;}
img{max-width: 100%;}
img a{border: none;}
ul{list-style-type: none;}

/* Fin Réinitialisation */

/* Démo */

body{
background: #f5f5f5;
padding-bottom: 30px;
}

.demo{
width: 600px;
margin: 100px auto 0 auto;
}

.demo h2{
font-weight: 300;
color: #444;
font-size: 1.4em;
border-bottom: 1px solid #444;
padding-bottom: 5px;
margin-bottom: 20px;
letter-spacing: 1px;
}

.demo p{
font-weight: 300;
color: #444;
font-size: 0.9em;
line-height: 25px;
margin-bottom: 40px;
text-align: justify;
}

.demo span{
display: block;
font-weight: 300;
font-style: italic;
font-size: 0.75em;
text-align: center;
color: #6a6a6a
}

.demo span a{
color: #6a6a6a
}

.demo span a:hover{
text-decoration: underline;
}

/* Old-Select */

.old-select{
position: absolute;
top: -9999px;
left: -9999px;
}

/* New-Select */

.new-select{
width: 300px;
height: 50px;
margin: auto;

margin-top: 50px;
text-align: center;
color: #444;
line-height: 50px;
position: relative;
}

.new-select .selection:active{
transform:         rotateX(42deg);
-o-transform:      rotateX(42deg);
-ms-transform:     rotateX(42deg);
-moz-transform:    rotateX(42deg);
-webkit-transform: rotateX(42deg);
transform-style:         preserve-3d;
-o-transform-style:      preserve-3d;
-ms-transform-style:     preserve-3d;
-moz-transform-style:    preserve-3d;
-webkit-transform-style: preserve-3d;
transform-origin:         top;
-o-transform-origin:      top;
-ms-transform-origin:     top;
-moz-transform-origin:    top;
-webkit-transform-origin: top;
transition:         transform         200ms ease-in-out;
-o-transition:      -o-transform      200ms ease-in-out;
-ms-transition:     -ms-transform     200ms ease-in-out;
-moz-transition:    -moz-transform    200ms ease-in-out;
-webkit-transition: -webkit-transform 200ms ease-in-out;
}

.new-select .selection{
width: 100%;
height: 100%;
background-color: #fff;
box-shadow: 0 1px 1px rgba(0,0,0,0.1);

cursor: pointer;
position: relative;
z-index: 20; /* Doit être supérieur au nombre d'option */

transform:         rotateX(0deg);
-o-transform:      rotateX(0deg);
-ms-transform:     rotateX(0deg);
-moz-transform:    rotateX(0deg);
-webkit-transform: rotateX(0deg);
transform-style:         preserve-3d;
-o-transform-style:      preserve-3d;
-ms-transform-style:     preserve-3d;
-moz-transform-style:    preserve-3d;
-webkit-transform-style: preserve-3d;
transform-origin:         top;
-o-transform-origin:      top;
-ms-transform-origin:     top;
-moz-transform-origin:    top;
-webkit-transform-origin: top;
transition:         transform         200ms ease-in-out;
-o-transition:      -o-transform      200ms ease-in-out;
-ms-transition:     -ms-transform     200ms ease-in-out;
-moz-transition:    -moz-transform    200ms ease-in-out;
-webkit-transition: -webkit-transform 200ms ease-in-out;
}

.new-select .selection p{
width: calc(100% - 60px);
position: relative;

transition:         all 200ms ease-in-out;
-o-transition:      all 200ms ease-in-out;
-ms-transition:     all 200ms ease-in-out;
-moz-transition:    all 200ms ease-in-out;
-webkit-transition: all 200ms ease-in-out;
}

.new-select .selection:hover p, .new-select .selection.open p{
color: #bdc3c7;
}

.new-select .selection i{
display: block;
width: 1px;
height: 70%;
position: absolute;
right: -1px; top: 15%;  bottom: 15%;
border: none;
background-color: #bbb;
}

.new-select .selection > span{
display: block;
width: 0;
height: 0;
border-style: solid;
border-width: 14px 8px 0 8px; /* Height: 14px / Width: 16px */
border-color: #bbb transparent transparent transparent;

position: absolute;
top: 18px; /* 50 / 2 - 14 / 2 */
right: 22px; /* 60 / 2 - 16 / 2 */
}

.new-select .selection.open > span{
width: 0;
height: 0;
border-style: solid;
border-width: 0 8px 14px 8px;
border-color: transparent transparent #bbb transparent;
}

.new-option{
text-align: center;
background-color: #fff;
cursor: pointer;
box-shadow: 0 1px 1px rgba(0,0,0,0.1);
position: relative;
margin-top: 1px;

position: absolute;
left: 0; right: 0;

transition:         all 300ms ease-in-out;
-o-transition:      all 300ms ease-in-out;
-ms-transition:     all 300ms ease-in-out;
-moz-transition:    all 300ms ease-in-out;
-webkit-transition: all 300ms ease-in-out;
}

.new-option p{
width: calc(100% - 60px);
}

.new-option.reveal:hover{
background-color: #444;
color: #f5f5f5;
}

Код: Выделить всё





HTML
CSS
SASS
Javascript
jQuery







[i][/i]





Initialisation
Chaque option du menu select (.old-select) est copiée afin de créer une nouvelle option dans le nouveau menu select (.new-select) qui portera dans un "data" sa "value". Le choix d'une option est ensuite fait afin de remplir le bloc ".selection". Si une des options porte l'attribut "selected", elle est choisis. Sinon, la première est sélectionnée. Une fois toutes les options "reconstruite", on inverse les z-index afin que la première soit devant, la deuxieèe ensuite, etc.

Ouverture / Fermeture
Les "new-option" étant positionnées en "absolute", on alterne simplement entre deux états avec des règles CSS différentes. Quelques règles diffèrent pour les trois dernières afin de respecter le design.

Sélection
Quand une "new-option" est selectionnée, ses données sont récupérées afin de :
- Indiquer la selection dans le "new-select"
- Intégrer l'attribut "selected" à l'option qui porte la même "value", après suppression pour celle qui le portait déjà.
Bien que visuellement, aucun changement ne se produit dans le "old-select", l'attribut est bien pris en compte lors de l'envoi du formulaire.

Design tiré du site [url=http://flatuicolors.com]flatuicolors.com[/url]



Подробнее здесь: https://stackoverflow.com/questions/793 ... -same-page
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «CSS»