JS触发器仅影响父母和兄弟姐妹

时间:2018-11-01 15:52:34

标签: javascript html css dom

上下文:我在工作之前和之后都有一个滑块部分,它定位一个类并影响父级或同级的CSS属性。但是,当我复制同一节时,第二个显然会影响第一个滑块。我希望能够多次复制我的版块,而不必每次都触摸代码。

我所知道的: querySelector只找到第一个元素。因此,我开始使用querySelectorAll,但是由于它影响到其他同级兄弟姐妹和父母,我开始感到非常困惑。

我想知道的是:如何使每个触发器仅影响其父级和同级财产?我应该为每个滑块创建不同的CSS类吗?

我所做的事情:我做了两个部分的Codepen,以使您对我的问题有更好的了解,并最终尝试一下。 https://codepen.io/greg_o/pen/KGOKvG

我的JS

let active = false;


var scrollers = document.querySelectorAll('.scroller');

scrollers.forEach(element => {
  element.addEventListener('mousedown',function scrolling(){
    active = true;
    element.classList.add('scrolling');
  });
});


document.body.addEventListener('mouseup',function(){
  active = false;
  document.querySelector('.scroller').classList.remove('scrolling');
});


document.body.addEventListener('mouseleave',function(){
  active = false;
  document.querySelector('.scroller').classList.remove('scrolling');
});


document.body.addEventListener('mousemove',function(e){
  if (!active) return;
  let x = e.pageX;
  x -= document.querySelector('.wrapper').getBoundingClientRect().left;
  scrollIt(x);
});


function scrollIt(x){
    let transform = Math.max(0,(Math.min(x,document.querySelector('.wrapper').offsetWidth)));
    document.querySelector('.after').style.width = 'calc(' + transform + 'px + 35vw)';
    document.querySelector('.scroller').style.left = transform-25+"px";
}


scrollIt(300);

我的CSS

#slider{
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .wrapper{
        width: 30vw;
        height: 20vw;
        position:relative;
        overflow:hidden;
        box-shadow: 0 10px 20px rgba(0,0,0,0.1), 0 6px 6px rgba(0,0,0,0.2);
    }

    .before, .after {
        width:100%;
        height:100%;
        background-repeat:no-repeat;
        background-color: white;
        background-size: cover;
        background-position: center;
        position: absolute;
        top:0;
        left:0;
        pointer-events:none;
        overflow: hidden;
    }

    .content-image{
        height:100%;
    }

    .after{
        transform: skewX(-30deg) translate(-35vw); 
    }

    .content-image.apres{
        transform: skewX(30deg) translate(35vw);
    }

    .scroller{
        width: 50px;
        height:50px;
        position: absolute;
        top:50%;
        transform:translateY(-50%);
        border-radius:50%;
        background-color: transparent;
        opacity:0.9;
        pointer-events:auto;
        cursor: move;
        box-sizing: border-box;
        border: 3px solid #fff;
    }

    .scroller:hover{
        opacity:1;
    }

    .scrolling{
        pointer-events:none;
        opacity:1;
        // z-index: 1;
    }

    .scroller__thumb{
        margin:0;
        box-sizing: border-box;
        width:100%;
        height:100%;
        padding:5px;
    }

    .scroller:before, .scroller:after{
        background: #fff;
        content:" ";
        display: block;
        width: 3px;
        height: 9999px;
        position: absolute;
        left: 50%;
        margin-left: -3.5px;
        z-index: 30;
        transition:0.1s;
    }
    .scroller:before{
        top:95%;
        left:28%;
        transform-origin:top;
        transform:rotate(30deg)
    }
    .scroller:after{
        bottom:95%;
        left:83%;
        transform-origin:bottom;
        transform:rotate(30deg)
    }

我的HTML

<section id="slider">
  <div class="wrapper">
  <div class="before">
    <img class="content-image" src="https://farm2.staticflickr.com/1638/26145024230_06acd55d1b_b.jpg" draggable="false"/>   </div>
  <div class="after">
    <img class="content-image apres" src="https://farm2.staticflickr.com/1663/25814974803_d4c55ff708_b.jpg" draggable="false"/>
  </div>
    <div class="scroller">
      <svg class="scroller__thumb" xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><polygon points="0 50 37 68 37 32 0 50" style="fill:#fff"/><polygon points="100 50 64 32 64 68 100 50" style="fill:#fff"/></svg>
    </div>
  </div>
</section>


<section id="slider">
  <div class="wrapper">
  <div class="before">
    <img class="content-image" src="https://farm2.staticflickr.com/1638/26145024230_06acd55d1b_b.jpg" draggable="false"/>   </div>
  <div class="after">
    <img class="content-image apres" src="https://farm2.staticflickr.com/1663/25814974803_d4c55ff708_b.jpg" draggable="false"/>
  </div>
    <div class="scroller">
      <svg class="scroller__thumb" xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><polygon points="0 50 37 68 37 32 0 50" style="fill:#fff"/><polygon points="100 50 64 32 64 68 100 50" style="fill:#fff"/></svg>
    </div>
  </div>
</section> 

2 个答案:

答案 0 :(得分:0)

我对您的代码笔进行了一些更改,现在可以正常工作了。

https://codepen.io/anon/pen/ZqgZmO

let active = false;


var scrollers = document.querySelectorAll('.scroller');
var sliders = document.querySelectorAll('.slider');

scrollers.forEach(element => {
  element.addEventListener('mousedown',function scrolling(){
    active = true;
    element.classList.add('scrolling');
  });
});

sliders.forEach(element => {
      element.addEventListener('mouseup',function(){
          active = false;
          element.querySelector('.scroller').classList.remove('scrolling');
     });

      element.addEventListener('mouseleave',function(){
          active = false;
          element.querySelector('.scroller').classList.remove('scrolling');
      });


      element.addEventListener('mousemove',function(e){
         if (!active) return;
              let x = e.pageX;
              x -= element.querySelector('.wrapper').getBoundingClientRect().left;
              scrollIt(x,element);
     });

});


function scrollIt(x, element){
    let transform = Math.max(0,(Math.min(x,document.querySelector('.wrapper').offsetWidth)));
    element.querySelector('.after').style.width = 'calc(' + transform + 'px + 35vw)';
    element.querySelector('.scroller').style.left = transform-25+"px";
}


scrollIt(300);

问题是,您为两个滑块使用相同的ID(id =“ slider”),并将事件添加到document.body。将来,请尝试将事件集中在一个元素上(在我们的情况下是滑块)。

希望有帮助。 最好的问候

答案 1 :(得分:0)

您可以通过在document.getElementsByClassName("scrolling")[0].parentNode方法中传递ScrollIt来解决问题。并使用该参数查找相关的.after.wrapper元素。

这里是codepen

let active = false;


var scrollers = document.querySelectorAll('.scroller');

scrollers.forEach(element => {
  element.addEventListener('mousedown',function scrolling(){
    active = true;
    element.classList.add('scrolling');
  });
});


document.body.addEventListener('mouseup',function(){
  active = false;
  document.querySelector('.scrolling').classList.remove('scrolling');
});


document.body.addEventListener('mouseleave',function(){
  active = false;
  document.querySelector('.scroller').classList.remove('scrolling');
});


document.body.addEventListener('mousemove',function(e){
  if (!active) return;
  let x = e.pageX;
  x -= document.getElementsByClassName("scrolling")[0].parentNode.getBoundingClientRect().left;
  scrollIt(document.getElementsByClassName("scrolling")[0].parentNode, x);
});


function scrollIt(elem, x){
    let transform = Math.max(0,(Math.min(x,elem.offsetWidth)));
    elem.querySelector('.after').style.width = 'calc(' + transform + 'px + 35vw)';
    elem.querySelector('.scroller').style.left = transform-25+"px";
}
scrollIt(document.getElementsByClassName("wrapper")[0] ,300);
scrollIt(document.getElementsByClassName("wrapper")[1] ,20);
#slider{
	  	display: flex;
	  	align-items: center;
	  	justify-content: center;
	}
		
	.wrapper{
	  	width: 30vw;
	  	height: 20vw;
	  	position:relative;
	  	overflow:hidden;
	  	box-shadow: 0 10px 20px rgba(0,0,0,0.1), 0 6px 6px rgba(0,0,0,0.2);
	}
	
	.before, .after {
	  	width:100%;
	  	height:100%;
	  	background-repeat:no-repeat;
	  	background-color: white;
	  	background-size: cover;
	  	background-position: center;
	  	position: absolute;
	  	top:0;
	  	left:0;
	  	pointer-events:none;
	  	overflow: hidden;
	}
	
	.content-image{
	  	height:100%;
	}
	
	.after{
	  	transform: skewX(-30deg) translate(-35vw); 
	}
	
	.content-image.apres{
	  	transform: skewX(30deg) translate(35vw);
	}
	
	.scroller{
	  	width: 50px;
	  	height:50px;
	  	position: absolute;
	  	top:50%;
	  	transform:translateY(-50%);
	  	border-radius:50%;
	  	background-color: transparent;
	  	opacity:0.9;
	  	pointer-events:auto;
	  	cursor: move;
	  	box-sizing: border-box;
	  	border: 3px solid #fff;
	}
	
	.scroller:hover{
	  	opacity:1;
	}
	
	.scrolling{
		pointer-events:none;
		opacity:1;
		// z-index: 1;
	}

	.scroller__thumb{
		margin:0;
		box-sizing: border-box;
		width:100%;
		height:100%;
		padding:5px;
	}
	
	.scroller:before, .scroller:after{
		background: #fff;
		content:" ";
		display: block;
		width: 3px;
		height: 9999px;
		position: absolute;
		left: 50%;
		margin-left: -3.5px;
		z-index: 30;
		transition:0.1s;
	}
	.scroller:before{
	  	top:95%;
	  	left:28%;
	  	transform-origin:top;
	  	transform:rotate(30deg)
	}
	.scroller:after{
	  	bottom:95%;
	  	left:83%;
	  	transform-origin:bottom;
	  	transform:rotate(30deg)
	}
<section id="slider">
  <div class="wrapper">
  <div class="before">
    <img class="content-image" src="https://farm2.staticflickr.com/1638/26145024230_06acd55d1b_b.jpg" draggable="false"/>   </div>
  <div class="after">
    <img class="content-image apres" src="https://farm2.staticflickr.com/1663/25814974803_d4c55ff708_b.jpg" draggable="false"/>
  </div>
    <div class="scroller">
      <svg class="scroller__thumb" xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><polygon points="0 50 37 68 37 32 0 50" style="fill:#fff"/><polygon points="100 50 64 32 64 68 100 50" style="fill:#fff"/></svg>
    </div>
  </div>
</section>


<section id="slider">
  <div class="wrapper">
  <div class="before">
    <img class="content-image" src="https://farm2.staticflickr.com/1638/26145024230_06acd55d1b_b.jpg" draggable="false"/>   </div>
  <div class="after">
    <img class="content-image apres" src="https://farm2.staticflickr.com/1663/25814974803_d4c55ff708_b.jpg" draggable="false"/>
  </div>
    <div class="scroller">
      <svg class="scroller__thumb" xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><polygon points="0 50 37 68 37 32 0 50" style="fill:#fff"/><polygon points="100 50 64 32 64 68 100 50" style="fill:#fff"/></svg>
    </div>
  </div>
</section>