Я написал несколько js, чтобы получить элемент по имени класса, но элемент, который я таргетинг, имеет два класса.Как использовать «getElementsByClassName» для получения нескольких классов из одного и того же элемента
<div class="module gh">
Это сценарий, который я использую, чтобы попытаться настроить этот класс.
var gh = document.getElementsByClassName("module gh");
По какой-то причине я получаю ошибку «неперехваченный TypeError: Невозможно установить свойство„-webkit-анимацию“неопределенный», который позволяет предположить, что класс не может быть найден. Что мне нужно сделать здесь?
Вот полный код, если кто-то хочет помочь мне очистить его и поделиться, почему они будут вносить изменения Я бы ВЕЛИКОЕ оценил это. Просто попасть в JS BTW ...
jQuery(document).ready(function(){jQuery('.carousel').each(function(index, element){jQuery(this)[index].slide = null;});jQuery('.carousel').carousel('cycle');});
var yPosition;
window.onload = function(){
yPosition = document.getElementById("sp-menu-wrapper").offsetTop;
}
window.onscroll = function(){
var stickyMenu = document.getElementById("sp-menu-wrapper");
var userOneSlideIn = document.getElementById("sp-user1");
var userTwoSlideIn = document.getElementById("sp-user2");
var userThreeSlideIn = document.getElementById("sp-user3");
var userFourSlideIn = document.getElementById("sp-user4");
var gh = document.getElementsByClassName("gh");
if(document.getElementById("sp-user1") != null && document.getElementById("sp-user2") != null && document.getElementById("sp-user3") != null && document.getElementById("sp-user4") != null && yPosition <= (window.pageYOffset + -100)){ // compare original y position of element to y position of page
userOneSlideIn.style["-webkit-animation"] = "flyin1 1s ease forwards";
userOneSlideIn.style["-moz-animation"] = "flyin1 1s ease forwards";
userOneSlideIn.style["-os-animation"] = "flyin1 1s ease forwards";
userOneSlideIn.style["-ms-animation"] = "flyin1 1s ease forwards";
userOneSlideIn.style["animation"] = "flyin1 1s ease forwards";
userOneSlideIn.style.marginLeft = "0px"
userTwoSlideIn.style["-webkit-animation"] = "flyin2 1s ease forwards";
userTwoSlideIn.style["-moz-animation"] = "flyin2 1s ease forwards";
userTwoSlideIn.style["-os-animation"] = "flyin2 1s ease forwards";
userTwoSlideIn.style["-ms-animation"] = "flyin2 1s ease forwards";
userTwoSlideIn.style["animation"] = "flyin2 1s ease forwards";
userTwoSlideIn.style.marginLeft = "30px"
userThreeSlideIn.style["-webkit-animation"] = "flyin3 1s ease forwards";
userThreeSlideIn.style["-moz-animation"] = "flyin3 1s ease forwards";
userThreeSlideIn.style["-os-animation"] = "flyin3 1s ease forwards";
userThreeSlideIn.style["-ms-animation"] = "flyin3 1s ease forwards";
userThreeSlideIn.style["animation"] = "flyin3 1s ease forwards";
userThreeSlideIn.style.marginLeft = "30px"
userFourSlideIn.style["-webkit-animation"] = "flyin4 1s ease forwards";
userFourSlideIn.style["-moz-animation"] = "flyin4 1s ease forwards";
userFourSlideIn.style["-os-animation"] = "flyin4 1s ease forwards";
userFourSlideIn.style["-ms-animation"] = "flyin4 1s ease forwards";
userFourSlideIn.style["animation"] = "flyin4 1s ease forwards";
userFourSlideIn.style.marginLeft = "30px"
}
if(document.getElementById("sp-menu-wrapper") != null && yPosition <= window.pageYOffset){
stickyMenu.style.position = "fixed";
stickyMenu.style.top = "0px";
stickyMenu.style.boxShadow= "rgb(112, 173, 139) 5px 5px 1200px";
stickyMenu.style.width= "100%";
stickyMenu.style.zIndex= "1";
}
else{
stickyMenu.style.position = "inherit";
stickyMenu.style.top = "";
}
if(document.getElementsByClassName("gh") != null && yPosition <= (window.pageYOffset + -400)){
gh.style["-webkit-animation"] = "ffr 2s ease forwards";
gh.style["-moz-animation"] = "ffr 2s ease forwards";
gh.style["-os-animation"] = "ffr 2s ease forwards";
gh.style["-ms-animation"] = "ffr 2s ease forwards";
gh.style["animation"] = "ffr 2s ease forwards";
gh.style.display = "block";
}
}
ПРИМЕЧАНИЕ: Вопрос был дан ответ, и вот мой модифицированный код для тех, кто имеет проблемы.
jQuery(document).ready(function(){//when document is ready - fire
var yPosition;//yPosition variable.
yPosition = document.getElementById("sp-menu-wrapper").offsetTop;// save original y position of element before scrolling.
window.onscroll = function(){// on scroll this will fire.
//declare all variables
var stickyMenu = document.getElementById("sp-menu-wrapper");
var userOneSlideIn = document.getElementById("sp-user1");
var userTwoSlideIn = document.getElementById("sp-user2");
var userThreeSlideIn = document.getElementById("sp-user3");
var userFourSlideIn = document.getElementById("sp-user4");
var gh = document.querySelectorAll(".module.gh");
//below - if element exists and scrlling is at this point - fire
if(document.getElementById("sp-user1") != null && document.getElementById("sp-user2") != null && document.getElementById("sp-user3") != null && document.getElementById("sp-user4") != null && yPosition <= (window.pageYOffset + -100)){ // compare original y position of element to y position of page
userOneSlideIn.style["-webkit-animation"] = "flyin1 1s ease forwards";//user1 style
userOneSlideIn.style["-moz-animation"] = "flyin1 1s ease forwards";
userOneSlideIn.style["-os-animation"] = "flyin1 1s ease forwards";
userOneSlideIn.style["-ms-animation"] = "flyin1 1s ease forwards";
userOneSlideIn.style["animation"] = "flyin1 1s ease forwards";
userOneSlideIn.style.marginLeft = "0px"
userTwoSlideIn.style["-webkit-animation"] = "flyin2 1s ease forwards";//user2 style
userTwoSlideIn.style["-moz-animation"] = "flyin2 1s ease forwards";
userTwoSlideIn.style["-os-animation"] = "flyin2 1s ease forwards";
userTwoSlideIn.style["-ms-animation"] = "flyin2 1s ease forwards";
userTwoSlideIn.style["animation"] = "flyin2 1s ease forwards";
userTwoSlideIn.style.marginLeft = "30px"
userThreeSlideIn.style["-webkit-animation"] = "flyin3 1s ease forwards";//user3 style
userThreeSlideIn.style["-moz-animation"] = "flyin3 1s ease forwards";
userThreeSlideIn.style["-os-animation"] = "flyin3 1s ease forwards";
userThreeSlideIn.style["-ms-animation"] = "flyin3 1s ease forwards";
userThreeSlideIn.style["animation"] = "flyin3 1s ease forwards";
userThreeSlideIn.style.marginLeft = "30px"
userFourSlideIn.style["-webkit-animation"] = "flyin4 1s ease forwards";//user4 style
userFourSlideIn.style["-moz-animation"] = "flyin4 1s ease forwards";
userFourSlideIn.style["-os-animation"] = "flyin4 1s ease forwards";
userFourSlideIn.style["-ms-animation"] = "flyin4 1s ease forwards";
userFourSlideIn.style["animation"] = "flyin4 1s ease forwards";
userFourSlideIn.style.marginLeft = "30px"
}
//below - if element exists and scrolling is at this point - fire
if(document.getElementById("sp-menu-wrapper") != null && yPosition <= window.pageYOffset){
//sticky menu style
stickyMenu.style.position = "fixed";
stickyMenu.style.top = "0px";
stickyMenu.style.boxShadow= "rgb(112, 173, 139) 5px 5px 1200px";
stickyMenu.style.width= "100%";
stickyMenu.style.zIndex= "1";
}
else{
//always show
stickyMenu.style.position = "inherit";
stickyMenu.style.top = "";
}
//below - if element exists and scrolling is at this point - fire
if(document.querySelectorAll(".module.gh") != null && yPosition <= (window.pageYOffset + -400)){
//loop through array
for(var g=0; g<gh.length; g++) {
gh[g].style["-webkit-animation"] = "ffr 2s ease forwards";//class="module gh"
gh[g].style["-moz-animation"] = "ffr 2s ease forwards";
gh[g].style["-os-animation"] = "ffr 2s ease forwards";
gh[g].style["-ms-animation"] = "ffr 2s ease forwards";
gh[g].style["animation"] = "ffr 2s ease forwards";
gh[g].style.display = "block";
}
}
}
//fire carousel onload
jQuery('.carousel').each(function(index, element){
jQuery(this)[index].slide = null;
});
jQuery('.carousel').carousel('cycle');
});
Если 'jQuery' в коде [jQuery] (http://api.jquery.com/), зачем использовать собственные запросы DOM? – Teemu
можете ли вы разместить html? – freshbm
Прошу прощения, я не уверен, что вы имеете в виду. Не могли бы вы рассказать? – thaGH05T