gpt4 book ai didi

javascript - 英雄 slider 最初为 : First slide does not load,

转载 作者:行者123 更新时间:2023-12-02 15:37:36 26 4
gpt4 key购买 nike

我不太熟悉 jQuery 和/或 JavaScript,但我正在尝试使用 Hero Slider对于我正在构建的网站。我几乎修改了代码来执行我想要的操作,但由于某种原因,第一张幻灯片似乎无法加载。奇怪的是,当 slider 循环回到起始幻灯片时,它显示得很好。如果我大胆猜测,我会说有些东西没有按正确的顺序加载,但我无法找出错误。 JQuery 在头部被调用,而脚本(下面的代码)在 </body> 之前被调用。 。我创建了一个JSFiddle如果有人能够提供关于为什么这不能正常工作的见解,我将非常感激。另外,虽然这不太重要,但如果有人能告诉我上 Angular 的小点来自哪里,我将不胜感激。

HTML

<section class="cd-hero">
<ul class="cd-hero-slider autoplay">
<li>
<div class="cd-full-width">
<h2>1. Slide title here</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Animi, explicabo.</p>
</div>
</li>
<li>
<div class="cd-full-width">
<h2>2. Slide title here</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Animi, explicabo.</p>
</div>
</li>
</ul>
</section>

JS

jQuery(document).ready(function($){
var slidesWrapper = $('.cd-hero-slider');

//check if a .cd-hero-slider exists in the DOM
if ( slidesWrapper.length > 0 ) {
var primaryNav = $('.cd-primary-nav'),
sliderNav = $('.cd-slider-nav'),
navigationMarker = $('.cd-marker'),
slidesNumber = slidesWrapper.children('li').length,
visibleSlidePosition = 0,
autoPlayId,
autoPlayDelay = 5000;

//upload videos (if not on mobile devices)
uploadVideo(slidesWrapper);

//autoplay slider
setAutoplay(slidesWrapper, slidesNumber, autoPlayDelay);

//on mobile - open/close primary navigation clicking/tapping the menu icon
primaryNav.on('click', function(event){
if($(event.target).is('.cd-primary-nav')) $(this).children('ul').toggleClass('is-visible');
});

//change visible slide
sliderNav.on('click', 'li', function(event){
event.preventDefault();
var selectedItem = $(this);
if(!selectedItem.hasClass('selected')) {
// if it's not already selected
var selectedPosition = selectedItem.index(),
activePosition = slidesWrapper.find('li.selected').index();

if( activePosition < selectedPosition) {
nextSlide(slidesWrapper.find('.selected'), slidesWrapper, sliderNav, selectedPosition);
} else {
prevSlide(slidesWrapper.find('.selected'), slidesWrapper, sliderNav, selectedPosition);
}

//this is used for the autoplay
visibleSlidePosition = selectedPosition;

updateSliderNavigation(sliderNav, selectedPosition);
updateNavigationMarker(navigationMarker, selectedPosition+1);
//reset autoplay
setAutoplay(slidesWrapper, slidesNumber, autoPlayDelay);
}
});
}

function nextSlide(visibleSlide, container, pagination, n){
visibleSlide.removeClass('selected from-left from-right').addClass('is-moving').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
visibleSlide.removeClass('is-moving');
});

container.children('li').eq(n).addClass('selected from-right').prevAll().addClass('move-left');
checkVideo(visibleSlide, container, n);
}

function prevSlide(visibleSlide, container, pagination, n){
visibleSlide.removeClass('selected from-left from-right').addClass('is-moving').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
visibleSlide.removeClass('is-moving');
});

container.children('li').eq(n).addClass('selected from-left').removeClass('move-left').nextAll().removeClass('move-left');
checkVideo(visibleSlide, container, n);
}

function updateSliderNavigation(pagination, n) {
var navigationDot = pagination.find('.selected');
navigationDot.removeClass('selected');
pagination.find('li').eq(n).addClass('selected');
}

function setAutoplay(wrapper, length, delay) {
if(wrapper.hasClass('autoplay')) {
clearInterval(autoPlayId);
autoPlayId = window.setInterval(function(){autoplaySlider(length)}, delay);
}
}

function autoplaySlider(length) {
if( visibleSlidePosition < length - 1) {
nextSlide(slidesWrapper.find('.selected'), slidesWrapper, sliderNav, visibleSlidePosition + 1);
visibleSlidePosition +=1;
} else {
prevSlide(slidesWrapper.find('.selected'), slidesWrapper, sliderNav, 0);
visibleSlidePosition = 0;
}
updateNavigationMarker(navigationMarker, visibleSlidePosition+1);
updateSliderNavigation(sliderNav, visibleSlidePosition);
}

function uploadVideo(container) {
container.find('.cd-bg-video-wrapper').each(function(){
var videoWrapper = $(this);
if( videoWrapper.is(':visible') ) {
// if visible - we are not on a mobile device
var videoUrl = videoWrapper.data('video'),
video = $('<video loop><source src="'+videoUrl+'.mp4" type="video/mp4" /><source src="'+videoUrl+'.webm" type="video/webm" /></video>');
video.appendTo(videoWrapper);
// play video if first slide
if(videoWrapper.parent('.cd-bg-video.selected').length > 0) video.get(0).play();
}
});
}

function checkVideo(hiddenSlide, container, n) {
//check if a video outside the viewport is playing - if yes, pause it
var hiddenVideo = hiddenSlide.find('video');
if( hiddenVideo.length > 0 ) hiddenVideo.get(0).pause();

//check if the select slide contains a video element - if yes, play the video
var visibleVideo = container.children('li').eq(n).find('video');
if( visibleVideo.length > 0 ) visibleVideo.get(0).play();
}

function updateNavigationMarker(marker, n) {
marker.removeClassPrefix('item').addClass('item-'+n);
}

$.fn.removeClassPrefix = function(prefix) {
//remove all classes starting with 'prefix'
this.each(function(i, el) {
var classes = el.className.split(" ").filter(function(c) {
return c.lastIndexOf(prefix, 0) !== 0;
});
el.className = $.trim(classes.join(" "));
});
return this;
};
});

最佳答案

最初,第一个<li>没有任何类(class)。如果添加<li class="selected from-right">到第一个<li> ,出现第一张幻灯片。

此外,要删除幻灯片之前的点,只需添加 ul{list-style-type:none;}在 CSS 中。

关于javascript - 英雄 slider 最初为 : First slide does not load,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32821521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com