Как интегрировать слайдер внутри невидимого контейнера, переключаемого кнопкой?CSS

Разбираемся в CSS
Ответить
Anonymous
 Как интегрировать слайдер внутри невидимого контейнера, переключаемого кнопкой?

Сообщение Anonymous »

Как новичок в HTML, CSS и JavaScript, я сейчас работаю над учебным проектом, в котором пытаюсь включить компонент слайдера в контейнер, который по умолчанию остается невидимым и становится видимым только при нажатии кнопки. нажал.
Я нашел руководство на YouTube (вот его исходный код) и выполнил шаги по интеграции слайдера на свою страницу.
Проблема, с которой я столкнулся, заключается в том, что слайдер работает идеально, когда я помещаю его непосредственно в тело страницы. Однако, когда я пытаюсь вложить его в контейнер наложения (хотя я не совсем уверен, что термин «наложение» здесь правильный), кажется, что он ведет себя не так, как ожидалось.
Вот код, который я хочу объединить из руководства

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






[*]

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

.container {
margin: 0 auto;
width: 60%;
height: 400px;
position: relative;
overflow: hidden;
}

.slides {
display: flex;
height: 100%;
}

.slide {
min-width: 100%;
position: relative;
}

.slide img {
width: 100%;
height: 100%;
}

.slide-controls {
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
}

#next-btn,
#prev-btn {
cursor: pointer;
background: transparent;
font-size: 30px;
border: none;
padding: 10px;
color: white;
}

#next-btn:focus,
#prev-btn:focus {
outline: none;
}

.slide-content {
position: absolute;
top: 50%;
left: 50px;
transform: translateY(-50%);
font-size: 60px;
color: white;
}

Infinite Slideshow : non-degree programmer








slide-1




slide-2


slide-3


slide-4




[/i]


[i][/i]




const slideContainer = document.querySelector('.container');
const slide = document.querySelector('.slides');
const nextBtn = document.getElementById('next-btn');
const prevBtn = document.getElementById('prev-btn');
const interval = 3000;

let slides = document.querySelectorAll('.slide');
let index = 1;
let slideId;

const firstClone = slides[0].cloneNode(true);
const lastClone = slides[slides.length - 1].cloneNode(true);

firstClone.id = 'first-clone';
lastClone.id = 'last-clone';

slide.append(firstClone);
slide.prepend(lastClone);

const slideWidth = slides[index].clientWidth;

slide.style.transform = `translateX(${-slideWidth * index}px)`;

console.log(slides);

const startSlide = () => {
slideId = setInterval(() => {
moveToNextSlide();
}, interval);
};

const getSlides = () => document.querySelectorAll('.slide');

slide.addEventListener('transitionend', () =>  {
slides = getSlides();
if (slides[index].id === firstClone.id) {
slide.style.transition = 'none';
index = 1;
slide.style.transform = `translateX(${-slideWidth * index}px)`;
}

if (slides[index].id === lastClone.id) {
slide.style.transition = 'none';
index = slides.length - 2;
slide.style.transform = `translateX(${-slideWidth * index}px)`;
}
});

const moveToNextSlide = () => {
slides = getSlides();
if (index >= slides.length - 1) return;
index++;
slide.style.transition = '.7s ease-out';
slide.style.transform = `translateX(${-slideWidth * index}px)`;
};

const moveToPreviousSlide = () => {
if (index  {
clearInterval(slideId);
});

slideContainer.addEventListener('mouseleave', startSlide);
nextBtn.addEventListener('click', moveToNextSlide);
prevBtn.addEventListener('click', moveToPreviousSlide);

startSlide();





Вот мой исходный код:

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

const questionContainer = document.getElementById('question-container');
const answerContainer = document.getElementById('answer-container');
const questionInput = document.getElementById('question-input');
const answerDiv = document.getElementById('answer');
const submitButton = document.getElementById('submit-button');
const clearButton = document.getElementById('clear-button');
const exploreButton = document.getElementById('explore');
const aboutButton = document.getElementById('about');
const overlay = document.getElementById("overlay");
const closeBtn = document.getElementById('closeBtn');
const exploreContent = document.getElementById('explore-content');
const aboutContent = document.getElementById('about-content');

questionInput.addEventListener('input', function () {
submitButton.disabled = !questionInput.value;
});

function handleAnswerSubmission() {
showAnswer();
questionInput.disabled = true;
submitButton.disabled = true;
}

function showAnswer() {
const question = questionInput.value;
const answer = question;
answerDiv.innerText = answer;
questionContainer.style.opacity = '.5';
questionContainer.style.top = '20%';
answerContainer.style.opacity = '1';
answerContainer.style.top = '50%';
answerContainer.style.visibility = 'visible';
submitButton.disabled = false;
}

function fetchData() {
// const apiUrl = '';
}

function handleClear() {
const question = questionInput.value;
questionContainer.style.opacity = '1';
questionContainer.style.top = '40%';
answerContainer.style.opacity = '0';
answerContainer.style.top = '80%';
answerContainer.style.visibility = 'visible';
questionInput.value = question;
questionInput.disabled = false;
submitButton.disabled = false;
questionInput.focus();
}

submitButton.addEventListener('click', handleAnswerSubmission);
questionInput.addEventListener('keypress', function (e) {
if (e.key === 'Enter' && questionInput.value && !overlay.classList.contains('active')) {
handleAnswerSubmission();
}
});

clearButton.addEventListener('click', handleClear);
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && !overlay.classList.contains('active')) {
handleClear();
}
});

window.addEventListener('load', function () {
fetchData();
});

exploreButton.addEventListener('click', function () {
showOverlay(exploreContent);
});

aboutButton.addEventListener('click', function () {
showOverlay(aboutContent);
});

closeBtn.addEventListener('click', () => {
hideOverlay();
});

function showOverlay(content) {
overlay.style.display = 'flex'; // Display the overlay
overlay.classList.add('active');  // Add the active class to start transition

// Hide all content initially
exploreContent.style.display = 'none';
aboutContent.style.display = 'none';

// Show the corresponding content
content.style.display = 'block';
}

function hideOverlay() {
overlay.classList.remove('active');
setTimeout(() => {
overlay.style.display = 'none';
exploreContent.style.display = 'none'; // Hide the content
aboutContent.style.display = 'none'; // Hide the content
}, 500);  // Adjust this time to match the CSS transition duration
}

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

* {
margin: 0;
padding: 0;
}

body {
display: flex;
align-items: center;
justify-content: center;
text-align: center;
/*font-size: 2em;*/
direction: rtl;
font-family: Arial, sans-serif;
color: #444444;
height: 100vh
}

#answer-container {
position: absolute;
top: 80%;
left: 50%;
transform: translate(-50%, -50%);
opacity: 0;
visibility: hidden;
max-width: 80%;
width: auto;
transition: opacity 1.15s ease, top 1.15s ease;
}

#question-container {
position: absolute;
top: 40%;
left: 50%;
transform: translate(-50%, -50%);
width: 65%;
height: auto;
transition: opacity 1s ease, top 1s ease;
}

#question-input {
width: 80%;
max-width: 90%;
padding: 10px;
font-size: 16px;
box-sizing: border-box;
word-wrap: break-word;
resize: none;
height: auto;
border: 1px solid #ccc;
}

#submit-button {
padding: 10px 20px;
font-size: 16px;
margin-top: 10px;
}

#clear-button-container {
display: flex;
justify-content: center;
align-items: center;
margin-top: 20px;
}

#clear-button {
padding: 10px 20px;
font-size: 16px;
background-color: rgb(185, 28, 28);
color: white;
border: none;
cursor: pointer;
}

#clear-button:hover {
background-color: rgb(172, 26, 26);
}

#submit-button:enabled {
cursor: pointer;
background-color: rgb(28, 185, 62);
color: white;
}

#answer {
width: 80%;
margin: 0 auto;
padding: 20px;
background-color: white;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

@media screen and (max-width: 600px) {
#question-input {
width: 90%;
}
}

.fab-container {
position: fixed;
bottom: 50px;
left: 50px;
z-index: 997;
cursor: pointer;
}

.fab-icon-holder {
width: 50px;
height: 50px;
border-radius: 100%;
background: #016fb9;
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

.fab-icon-holder:hover {
opacity: 0.8;
}

.fab-icon-holder i {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
color: #fff;
}

.fab {
width: 60px;
height: 60px;
background: #016fb9;
}

.fab-options {
list-style-type: none;
margin: 0;
position: absolute;
bottom: 70px;
left: 0;
opacity: 0;
transition: all 0.5s ease;
transform: scale(0);
transform-origin: 20% bottom;
}

.fab:hover+.fab-options,
.fab-options:hover {
opacity: 1;
transform: scale(1);
}

.fab-options li {
display: flex;
justify-content: flex-end;
padding: 5px;
}

.fab-label {
padding: 2px 5px;
align-self: center;
user-select: none;
white-space: nowrap;
border-radius: 3px;
font-size: 16px;
background: #666;
color: #fff;
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
margin-left: 10px;
}

.overlay {
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, 0);
/* Initially transparent */
z-index: 998;
justify-content: center;
align-items: center;
opacity: 0;
transition: opacity 0.5s ease;
/* Smooth transition effect */
display: none;
/* Initially hidden */
}

.overlay.active {
background-color: rgba(0, 0, 0, 0.7);
/* Semi-transparent background */
opacity: 1;
/* Display when active */
}

.explore-content,
.about-content {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
display: none;
/* Initially hidden */
z-index: 999;
color: black;
width: 90%;
/* Set the width to 80% of the screen */
height: 50%;
/* Set the width to 80% of the screen */
}

#closeBtn {
position: absolute;
top: 20px;
right: 20px;
cursor: pointer;
}

.fa-circle-xmark {
font-size: 32px;
color:  white;
}

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






Title






[i]

[/i]





[i][/i]





[i][/i]

[list]

questions list

[i][/i]

[*]
about

[i][/i]

[/list]



[i][/i]


I want the slider to be here



This is the about section.








Подробнее здесь: https://stackoverflow.com/questions/783 ... y-a-button
Ответить

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

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

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

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

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