简体   繁体   English

JS更改高度并返回原始CSS高度

[英]JS change height and return to original CSS height

Hoping to get some help on some javascript i'm getting stuck with. 希望对我陷入困境的某些JavaScript有所帮助。

Q1: JS RETURN TO IMAGE HEIGHT AND MARGIN Q1:JS返回图像高度和边距

My layout is horizontal scroll of smaller images positioned in a grid, using different height percentages and margins to position. 我的布局是将较小图像的水平滚动放置在网格中,并使用不同的高度百分比和边距进行定位。 When you click on any of the images they all expand to height:100% and margin:0 which clears all previous styles putting it into a simple large image layout. 当您单击任何图像时,它们都将扩展到height:100%和margin:0:这将清除所有以前的样式,并将其放入简单的大图像布局中。

My question is how do I add a function that when clicking on .image-container the height and margins returns to how it was originally set in the css 我的问题是如何添加一个函数,当单击.image-container时,高度和边距将恢复为最初在CSS中设置的方式

JS FIDDLE DEMO (click any center image) JS FIDDLE DEMO (单击任何中心图像)

// GALLERY 100% height
    $('.image-container').click(function(){
        $('.image-container img').animate({'height': '100%'},900) 
        .animate({'margin': '0'},900); 

    });

    // ? REMOVE HEIGHT ? 
    $('.image-container').click(function(){
        $('.image-container img').animate({'height': '?'},900)
        .animate({'margin': '?'},900); 

    });

EDIT UPDATED QUESTION: Q2 HOW TO MAKE PAGE SIZE GROW WITH LARGER IMAGES 编辑更新的问题:Q2如何使页面尺寸以更大的图像增长

Right now my .image-container is set to a large width but with responsive images it's hard to find the correct width, is there a way to find this width and to make it grow along with the click grow of images (displayed above) 现在,我的.image-container设置为较大的宽度,但是对于响应图像,很难找到正确的宽度,有没有一种方法可以找到此宽度并使它随着图像的点击增长而增长(如上所示)

.image-container {
display:block;
width:3600px;
height: 75%;
float:left;

position:absolute;
top: 13%;
left: 120px;

z-index: -1;

} }

Thanks for any help! 谢谢你的帮助!

You need to store the original height in a variable. 您需要将原始高度存储在变量中。

Check out the updated fiddle 查看更新的小提琴

var originalheight;
        // GALLERY 100% height
        $('.image-container').click(function(){
            originalheight = $('.image-container img').height();
            $('.image-container img').animate({'height': '100%'},900) 
            .animate({'margin': '0'},900); 

        });

        //REMOVE HEIGHT ?
        $('.image-container').click(function(){
            $('.image-container img').animate({'height': originalheight},900)
            .animate({'margin': '0'},900); 

        });

EDIT: 编辑:

Sorry about the goof up in previous solution. 抱歉,先前的解决方案存在问题。 I didn't notice I was using click twice unnecessarily. 我没有注意到我不必要地使用了两次click Here's the updated solution with the updated fiddle . 这是带有更新的小提琴的更新的解决方案。

       var originalheight;
       $('.image-container').click(function () {
           if (!originalheight) originalheight = $('.image-container img').height();
           if ($('.image-container img').css('height') == originalheight + "px") { // GALLERY 100% height
               $('.image-container img').animate({
                   'height': '100%'
               }, 900).animate({
                   'margin': '0'
               }, 900);
           } else { //REMOVE HEIGHT ?
               $('.image-container img').animate({
                   'height': originalheight
               }, 900).animate({
                   'margin': '0'
               }, 900);
           }
       });

This is my idea, hope it'll work: 这是我的想法,希望它能起作用:

// Before animation
    var heights = new Array();
    var margins = new Array();
    $('.image-container img').each(function(){
       heights.push($(this).css('height'));
    });
    $('.image-container img').each(function(){
       margins.push($(this).css('margin'));
    });
// 
    $('.image-container').click(function(){
        $('.image-container img').animate({'height': '100%'},900) 
        .animate({'margin': '0'},900); 

    });

    // ? REMOVE HEIGHT ? 
    $('.image-container').click(function(){
        $('.image-container img').animate({'height': heights[$(this).index()]},900)
        .animate({'margin': margins[$(this).index()]},900); 

    });

First of all, I suggest you to use the focusout and blur event to undo changes, because the way you implement your 'click' event, the second part only will be considered (you erase the first click implementation doing so). 首先,我建议您使用focusout和blur事件来撤消更改,因为实现“单击”事件的方式仅考虑第二部分(这样做是为了擦除第一单击实现)。 the best thing, is to enlarge the image when clicked, and reinit it's size when you click away. 最好的办法是单击时放大图像,并在单击时重新设置其大小。

Try this : 尝试这个 :

// GALLERY 100% height
$('.image-container')
  .bind('click', function(){
    $('.image-container img').animate({height: '100%'},900) 
    .animate({margin: 0},900);
  })
  .bind('focusout blur', function(){
    $('.image-container img').animate({height: ''},900) 
    .animate({margin: ''},900);
  });

Or better, use classes in which you define the behaiviors on clicking, and on clicking again, for exemple : 或者更好的方法是,使用在其中定义行为的类,例如,在单击和再次单击时,定义行为:

<style>
.clickimage{ height : 100%; margin :0;}
.yourOriginalValues {height : original; margin : original;}
</style>

  $('.yourOriginalValues').click(function(){
    $(this).switchClass( "yourOriginalValues", "clickimage", 900 );
  });
  $('.clickimage).click(function(){
    $(this).switchClass( "clickimage", "yourOriginalValues", 900 );
  });      

ps. PS。 the switchClass method, is a jQuery UI functionality. switchClass方法是jQuery UI功能。

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

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