full
This commit is contained in:
@@ -2,14 +2,18 @@
|
||||
|
||||
/**
|
||||
* Переключает язык (регионы) необходим плагин Polylang
|
||||
*
|
||||
* Поддерживает множественные вызовы на одной странице
|
||||
*/
|
||||
|
||||
|
||||
// Проверяем существование функций Polylang
|
||||
if (!function_exists('pll_languages_list') || !function_exists('pll_current_language')) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
static $instance_counter = 0;
|
||||
$instance_counter++;
|
||||
$unique_id = 'plLangSwitcher' . $instance_counter;
|
||||
?>
|
||||
|
||||
<style>
|
||||
@@ -19,8 +23,6 @@ if (!function_exists('pll_languages_list') || !function_exists('pll_current_lang
|
||||
border: 1px solid #e0e0e0;
|
||||
border-radius: 90px;
|
||||
padding: 6px;
|
||||
--pl-slider-width: 0px;
|
||||
--pl-slider-x: 0px;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
@@ -61,11 +63,6 @@ if (!function_exists('pll_languages_list') || !function_exists('pll_current_lang
|
||||
color: #b9b7b9;
|
||||
}
|
||||
|
||||
.pl-lang-switcher-button:hover,
|
||||
.pl-lang-switcher-container a(not:.pl-lang-switcher-current):hover {
|
||||
color: #222;
|
||||
}
|
||||
|
||||
|
||||
.pl-lang-switcher-button.pl-lang-switcher-active,
|
||||
.pl-lang-switcher-container a.pl-lang-switcher-active,
|
||||
@@ -83,8 +80,6 @@ if (!function_exists('pll_languages_list') || !function_exists('pll_current_lang
|
||||
background: linear-gradient(90deg, #2b2c35 67.31%, #4f5870 92.9%);
|
||||
border-radius: 90px;
|
||||
z-index: 1;
|
||||
width: var(--pl-slider-width);
|
||||
transform: translateX(var(--pl-slider-x));
|
||||
transition: all 180ms cubic-bezier(0.4, 0.0, 0.2, 1);
|
||||
}
|
||||
|
||||
@@ -93,8 +88,8 @@ if (!function_exists('pll_languages_list') || !function_exists('pll_current_lang
|
||||
}
|
||||
</style>
|
||||
|
||||
<div class="pl-lang-switcher-container" id="plLangSwitcherContainer">
|
||||
<div class="pl-lang-switcher-slider" id="plLangSwitcherSlider"></div>
|
||||
<div class="pl-lang-switcher-container" data-switcher-id="<?php echo $unique_id; ?>">
|
||||
<div class="pl-lang-switcher-slider"></div>
|
||||
<?php
|
||||
$languages = pll_languages_list();
|
||||
$current_lang = pll_current_language('slug');
|
||||
@@ -146,9 +141,15 @@ if (!function_exists('pll_languages_list') || !function_exists('pll_current_lang
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const container = document.getElementById('plLangSwitcherContainer');
|
||||
if (!container) return;
|
||||
// Инициализируем все переключатели языков на странице
|
||||
const containers = document.querySelectorAll('.pl-lang-switcher-container[data-switcher-id="<?php echo $unique_id; ?>"]');
|
||||
|
||||
containers.forEach(function(container) {
|
||||
initLanguageSwitcher(container);
|
||||
});
|
||||
});
|
||||
|
||||
function initLanguageSwitcher(container) {
|
||||
let isInitialized = false;
|
||||
|
||||
function updateSliderPosition(target = null, animate = false) {
|
||||
@@ -157,7 +158,7 @@ if (!function_exists('pll_languages_list') || !function_exists('pll_current_lang
|
||||
}
|
||||
if (!target) return;
|
||||
|
||||
const slider = container.querySelector('#plLangSwitcherSlider');
|
||||
const slider = container.querySelector('.pl-lang-switcher-slider');
|
||||
const width = target.offsetWidth;
|
||||
const x = target.offsetLeft - 6;
|
||||
|
||||
@@ -167,8 +168,8 @@ if (!function_exists('pll_languages_list') || !function_exists('pll_current_lang
|
||||
slider.style.transition = 'none';
|
||||
}
|
||||
|
||||
container.style.setProperty('--pl-slider-width', width + 'px');
|
||||
container.style.setProperty('--pl-slider-x', x + 'px');
|
||||
slider.style.width = width + 'px';
|
||||
slider.style.transform = 'translateX(' + x + 'px)';
|
||||
}
|
||||
|
||||
function initialize() {
|
||||
@@ -208,8 +209,8 @@ if (!function_exists('pll_languages_list') || !function_exists('pll_current_lang
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
// Обновляем только в текущем контейнере
|
||||
container.querySelectorAll('a').forEach(a => a.classList.remove('pl-lang-switcher-current'));
|
||||
|
||||
link.classList.add('pl-lang-switcher-current');
|
||||
|
||||
updateSliderPosition(link, true);
|
||||
@@ -219,5 +220,5 @@ if (!function_exists('pll_languages_list') || !function_exists('pll_current_lang
|
||||
}
|
||||
|
||||
initialize();
|
||||
});
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user