Jquery和Javascript从外部divs点击滚动图像

时间:2017-02-05 01:29:07

标签: javascript jquery html css

我有一个灯箱,我可以在div上创建一个带有图像的div,然后在新的div中使用灯箱效果打开更大的图像。我想要箭头,你可以点击向前滚动并返回其他外部div中的图像。我是JQuery和javascript的新手,我已经尝试了我能想到的各种方式,而我只是错过了如何让它工作的东西。我附上了我的代码的基础知识作为参考。

高和显示代码无法正常工作,因为它由于某种原因没有检测到其他div。

$(document).ready(function() {
	$("img").click(function(){
		$src=$(this).attr("src");
		$title=$(this).attr("title");
		$alt=$(this).attr("alt");


if(!$("#lightbox").length>0){
			$('body').append("<div id='lightbox'><span class='closer'>X</span><span class='nextimg'>></span><span class='previmg'><</span><div class='lightbox_container'><img src='' title='' alt=''><p>" + $title + "</p></div></div>");
			

			$("#lightbox").show();
			
			
			
			
			//*********************************************************
			if(!$(this).parent("#lightboxsm img").next().length>0){
				$(".nextimg").show();
			} else {
				$(".nextimg").hide();
			};
			
			$("body").on('click','.nextimg',function(){
				$newsrc=$("#lightbox img").attr("src");
				$newtitle=$("#lightbox img").attr("title");
				$newalt=$("#lightbox img").attr("alt");
				
				
				$("#lightboxsm img").next("lightbox img").attr('src', $newsrc);
				
			});
			
			//*********************************************************
  
  }else{
			$("#lightbox img").attr("src",$src);
			$("#lightbox img").attr("alt",$alt);
			$("#lightbox img").attr("title",$title);
			$("#lightbox").show();
		}
	});
	
	
	//Closes the lightbox***************************************
	$("body").on('click', '#lightbox .closer',function(){
		$("#lightbox").hide();
	})
    
    });
.lightboxsm {
	width: 175px;
    height: 175px;
    overflow: hidden;
	/*float:left;*/
	display:inline-block;
	padding:10px;
	position:relative;
	cursor:pointer;
}

.lightboxsm img{
	width:auto;
    height: 175px;
	object-fit: cover;
}

#lightbox{
	position:fixed;
	height:100%;
	width:100%;
	background: rgba(0, 0, 0, 0.5);
	left:0;
	right:0;
	top:0;
	bottom:0;
	z-index:200;
}

#lightbox img{
	max-width:80%;
	max-height:80%;
	position:fixed;
	left:0;
	right:0;
	top:0;
	bottom:0;
	margin:auto;
}

.closer {
	display: block;
    height: 60px;
    width: 60px;
    line-height: 60px;

    -moz-border-radius: 30px;
    border-radius: 30px;

    background-color: black;
    color: white;
    text-align: center;
    font-size: 2em;
	float:right;
	margin:5% 10%;
	z-index:250;
}

.closer:hover {
	cursor:pointer;
}

.nextimg {
	display: block;
    height: 60px;
    width: 60px;
    line-height: 60px;

    -moz-border-radius: 30px;
    border-radius: 30px;

    background-color: black;
    color: white;
    text-align: center;
    font-size: 2em;
	float:right;
	margin:5% 10%;
	z-index:600;
	clear:right;
}

.nextimg:hover {
	cursor:pointer;
}

.previmg {
	display: block;
    height: 60px;
    width: 60px;
    line-height: 60px;

    -moz-border-radius: 30px;
    border-radius: 30px;

    background-color: black;
    color: white;
    text-align: center;
    font-size: 2em;
	float:left;
	margin:5% 10%;
	z-index:600;
	clear:left;
}

.previmg:hover {
	cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="lightbox_container">
            <div class="lightboxsm" id="img1">
                <img src="http://2017.sunkissed-villas.com/images/1.png" alt="1"/>
            </div>
        
            <div class="lightboxsm" id="img2">
                <img src="http://2017.sunkissed-villas.com/images/2.png" alt="2"/>
            </div>
            
            <div class="lightboxsm" id="img3">
                <img src="http://2017.sunkissed-villas.com/images/3.png"/>
            </div>
            
        </div>

1 个答案:

答案 0 :(得分:1)

问题是nextimg选择器在获取next()之前没有访问父级,因此没有下一个元素。如果更新的代码段有效,请告诉我。如果没有下一个或上一个图像,我也是这样做的,它隐藏了下一个/上一个按钮。

$(document).ready(function() {
  $("img").click(function(){
    $(this).addClass('selected')
    $src=$(this).attr("src");
    $title=$(this).attr("title");
    $alt=$(this).attr("alt");


    if(!$("#lightbox").length > 0){
      $('body').append("<div id='lightbox'><span class='closer'>X</span><span class='nextimg'>></span><span class='previmg'><</span><div class='lightbox_container'><img src='' title='' alt=''><p>" + $title + "</p></div></div>");
    			
      $("body").on('click','.nextimg',function(){
        $newimg=$('.selected').parent().next().find('img')
        $('.selected').removeClass('selected')
        $newimg.addClass('selected')
    	$("#lightbox img").attr("src", $newimg.attr("src"));
        checkPrevNext();
      });
      
      $("body").on('click','.previmg',function(){
        $newimg=$('.selected').parent().prev().find('img')
        $('.selected').removeClass('selected')
        $newimg.addClass('selected')
    	$("#lightbox img").attr("src", $newimg.attr("src"));
        checkPrevNext();
      });
    }
      $("#lightbox").show();
      $("#lightbox img").attr("src",$src);
      $("#lightbox img").attr("alt",$alt);
      $("#lightbox img").attr("title",$title);
      checkPrevNext();
   });
    	
  $("body").on('click', '#lightbox .closer',function(){
    $("#lightbox").hide();
  })
  
  function checkPrevNext() {
    if($('.selected').parent().next().find('img').length > 0) {
      $(".nextimg").show();
    } else {
      $(".nextimg").hide();
    };
    
    if($('.selected').parent().prev().find('img').length > 0) {
      $(".previmg").show();
    } else {
      $(".previmg").hide();
    };
  }
});
.lightboxsm {
	width: 175px;
    height: 175px;
    overflow: hidden;
	/*float:left;*/
	display:inline-block;
	padding:10px;
	position:relative;
	cursor:pointer;
}

.lightboxsm img{
	width:auto;
    height: 175px;
	object-fit: cover;
}

#lightbox{
	position:fixed;
	height:100%;
	width:100%;
	background: rgba(0, 0, 0, 0.5);
	left:0;
	right:0;
	top:0;
	bottom:0;
	z-index:200;
}

#lightbox img{
	max-width:80%;
	max-height:80%;
	position:fixed;
	left:0;
	right:0;
	top:0;
	bottom:0;
	margin:auto;
}

.closer {
	display: block;
    height: 60px;
    width: 60px;
    line-height: 60px;

    -moz-border-radius: 30px;
    border-radius: 30px;

    background-color: black;
    color: white;
    text-align: center;
    font-size: 2em;
	float:right;
	margin:5% 10%;
	z-index:250;
}

.closer:hover {
	cursor:pointer;
}

.nextimg {
	display: block;
    height: 60px;
    width: 60px;
    line-height: 60px;

    -moz-border-radius: 30px;
    border-radius: 30px;

    background-color: black;
    color: white;
    text-align: center;
    font-size: 2em;
	float:right;
	margin:5% 10%;
	z-index:600;
	clear:right;
}

.nextimg:hover {
	cursor:pointer;
}

.previmg {
	display: block;
    height: 60px;
    width: 60px;
    line-height: 60px;

    -moz-border-radius: 30px;
    border-radius: 30px;

    background-color: black;
    color: white;
    text-align: center;
    font-size: 2em;
	float:left;
	margin:5% 10%;
	z-index:600;
	clear:left;
}

.previmg:hover {
	cursor:pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="lightbox_container">
  <div class="lightboxsm" id="img1">
    <img src="http://2017.sunkissed-villas.com/images/1.png" alt="1"/>
  </div>
        
  <div class="lightboxsm" id="img2">
    <img src="http://2017.sunkissed-villas.com/images/2.png" alt="2"/>
  </div>
            
  <div class="lightboxsm" id="img3">
    <img src="http://2017.sunkissed-villas.com/images/3.png"/>
  </div>            
</div>