简体   繁体   English

为什么css margin会在点击按钮时隐藏图像?

[英]Why css margin hide the image on click of button?

I try to scroll my images using next and previous button. 我尝试使用下一个上一个按钮滚动我的图像。 I used CSS to do the thing. 我用CSS做了这件事。 I am setting the margin of ul to get next and previous image but when I click on next button it hides my image instead of showing the next image. 我设置ul的边距以获得下一个和上一个图像,但是当我点击下一个按钮时,它隐藏了我的图像而不是显示下一个图像。 Could you please tell me how to show next image ? 你能告诉我如何显示下一张图片吗?

Here is my code: 这是我的代码:

 $(function() { $('#next').click(function() { alert('next'); $('.outer ul').css('margin-left', '200px'); }); $('#pre').click(function() { alert('pre'); $('.outer ul').css('margin-left', '-200px'); }); }); 
 .outer { width: 200px; overflow: hidden } .outer ul { width: 600px; margin: 0 auto 0 auto; } .outer li { display: inline-block; width: 195px } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="outer"> <ul> <li> <img alt="" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQITZTZxY9nTV6pm2M0lbyXVt03SyyD9I1Th1aJ8UfeFbF2FE0oXLoUKUDd"> </li> <li> <img alt="" src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcT7lw5rwUWCsmOFSTUuFF84niMBZg6J9KeWhws1Ysib3VdVTM8-RA"> </li> <li> <img alt="" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQITZTZxY9nTV6pm2M0lbyXVt03SyyD9I1Th1aJ8UfeFbF2FE0oXLoUKUDd"> </li> </ul> <button id='pre'> previous </button> <button id='next'> next </button> </div> 

$(function(){
var nextprevmargin = 0;
$('#next').click(function(){
    nextprevmargin = parseInt(nextprevmargin) - 200;
    $('.outer ul').css('margin-left',nextprevmargin + 'px');
})
$('#pre').click(function(){
    nextprevmargin = parseInt(nextprevmargin) + 200;
    $('.outer ul').css('margin-left',nextprevmargin + 'px');
})

})

here is fiddle for you. 这里是你的小提琴。 https://jsfiddle.net/urLdLdLm/9/ You have to add your counter check for prevention of scroll to be go out of screen https://jsfiddle.net/urLdLdLm/9/您必须添加计数器检查以防止滚动出屏

Try to use +=200 and -=200 values as the parameter of .css() methods. 尝试使用+=200-=200值作为.css()方法的参数。

 $(function(){ $('#next').click(function(){ alert('next'); console.log() $('.outer ul').css('margin-left','-=200') $ }) $('#pre').click(function(){ alert('pre') $('.outer ul').css('margin-left','+=200') }) }) 
 .outer { width:200px; overflow:hidden; } .outer ul { width: 600px; margin: 0 auto 0 auto; } .outer li { display: inline-block; width:195px } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="outer"> <ul> <li><img alt="" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQITZTZxY9nTV6pm2M0lbyXVt03SyyD9I1Th1aJ8UfeFbF2FE0oXLoUKUDd"></li> <li><img alt="" src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcT7lw5rwUWCsmOFSTUuFF84niMBZg6J9KeWhws1Ysib3VdVTM8-RA"></li> <li><img alt="" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQITZTZxY9nTV6pm2M0lbyXVt03SyyD9I1Th1aJ8UfeFbF2FE0oXLoUKUDd"></li> </ul> <button id='pre'> previous </button> <button id='next'> next </button> </div> 

When you're calling .css('margin-left','200px') or .css('margin-left','-200px') , you're setting the margin left to that value, not adding or subtracting from the current value. 当您调用.css('margin-left','200px').css('margin-left','-200px') ,您将剩余边距设置为该值,而不是添加或减去当前的价值。

Also, the next button should be subtracting, not adding and vice versa for the previous button. 此外,下一个按钮应该减去,而不是添加,反之亦然。

Here's a jsFiddle of it working. 这是一个工作的jsFiddle

You're gonna need to set some bounds on this as well, so that the user doesn't add or subtract from the margin-left ad infinitum. 你也需要在这方面设置一些界限,这样用户就不会无限地添加或减少边距左边的广告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM