.load之后,div不会改变高度,但第二秒之后它会改变

发布于 2024-09-19 17:04:38 字数 683 浏览 16 评论 0原文

让我们直接进入代码:

$(".btn-slide").live("click", function(e){

  if (e.preventDefault) { 
   e.preventDefault(); 
  } 
  else { 
   e.returnValue = false; 
  }

  //deo za dobavljanje odgovarajuce stranice
  link = $(this).attr("href");

  if($('#post-content').hasClass("active")){
   $("#post-content").slideToggle("slow");
   $('#post-content').toggleClass("active");
  }

  $('#post-content').load(link);

  $("#post-content").slideToggle("slow");
  $('#post-content').toggleClass("active");

  $('#content').height($('#post-content').height()); return false;
 });

因此,我通过单击将一些内容加载到当前页面/div 中,但内容 div 在第一次调用后不会调整大小。但是当我再次单击该链接时,它会调整大小...... 有人解决吗?

Let's get straight to the code:

$(".btn-slide").live("click", function(e){

  if (e.preventDefault) { 
   e.preventDefault(); 
  } 
  else { 
   e.returnValue = false; 
  }

  //deo za dobavljanje odgovarajuce stranice
  link = $(this).attr("href");

  if($('#post-content').hasClass("active")){
   $("#post-content").slideToggle("slow");
   $('#post-content').toggleClass("active");
  }

  $('#post-content').load(link);

  $("#post-content").slideToggle("slow");
  $('#post-content').toggleClass("active");

  $('#content').height($('#post-content').height()); return false;
 });

So, I'm loading some contents into my current page/div on a click, but the content div just won't resize after the first call. But when I click the link again, it resizes...
Solution anybody?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

君勿笑 2024-09-26 17:04:38

您的具体问题尚不清楚,但问题的原因很清楚。这与活跃阶级有关。因为我们看不到活动类是什么——我不能肯定地说。但让我们看看会发生什么:

Case 1 -- #post-content starts having active

 - Button is pressed
 - (slide is toggled)
 - Active is removed
 - Content is loaded
 - (slide is toggled)
 - Active is turned back on.


Case 2 -- #post-content starts not having active

 - Button is pressed
 - Content is loaded
 - (slide is toggled)
 - Active is turned on.

基于此
如果 active 启动,则第一次运行后不会发生任何变化。

如果活动开始,则案例 2 运行一次,案例 1 从此开始运行。

听起来您想确保案例 1 是唯一运行的事情,确保 #post-content 开始上课活跃,你应该没问题。

另一方面,您的代码没有多大意义,因此您可能想要更改它、添加注释、重构等。

Your exact problem is not clear, but the cause of your problem is clear. It has to do with the active class. Since we can't see what the active class is -- I can't say for sure. But lets look at what happens:

Case 1 -- #post-content starts having active

 - Button is pressed
 - (slide is toggled)
 - Active is removed
 - Content is loaded
 - (slide is toggled)
 - Active is turned back on.


Case 2 -- #post-content starts not having active

 - Button is pressed
 - Content is loaded
 - (slide is toggled)
 - Active is turned on.

Based on this
If active starts on there is no change after the first run.

If active starts off, then case 2 gets run once and case 1 gets run from then on.

Sounds like you want to make sure case 1 is the only thing run make sure #post-content starts having class active and you should be fine.

On the other hand, your code does not make so much sense, so you might want to change it, add comments, re-factor, etc.

定格我的天空 2024-09-26 17:04:38

问题是 $('#post-content').height() 是未知的,直到 SlideToggle 动画完成。因此,只需将调整大小逻辑放入 SlideToggle 的回调函数中即可解决该问题。这是对我有用的代码:

$(".btn-slide").live("click", function(e){

        link = $(this).attr("href");

        if($('#post-content').hasClass("active")){
            $("#post-content").slideToggle("slow");
            $('#post-content').toggleClass("active");
        }

        $('#post-content').load(link);

        $("#post-content").slideToggle("slow", function(){
            //deo za poravnavanje visina
            h=0;
            //velicina contenta zbog accordiona
            $('.sidebar-arch').children().each(function() {
                h = h + $(this).outerHeight(true) + 20;
            });

            $('#content').height(h);

            if($('#post-content').height() > $('#content').height()){
                $('#content').height($('#post-content').height());
            }

            $('#post-content').toggleClass("active");
        });

        return false;
    });

谢谢大家的评论。我会学习更好地组织我的代码^_^

The problem was that the $('#post-content').height() is unknown until the slideToggle animation finishes. So just putting the resize logic inside a callback function of the slideToggle solved that. Here is the code that worked for me:

$(".btn-slide").live("click", function(e){

        link = $(this).attr("href");

        if($('#post-content').hasClass("active")){
            $("#post-content").slideToggle("slow");
            $('#post-content').toggleClass("active");
        }

        $('#post-content').load(link);

        $("#post-content").slideToggle("slow", function(){
            //deo za poravnavanje visina
            h=0;
            //velicina contenta zbog accordiona
            $('.sidebar-arch').children().each(function() {
                h = h + $(this).outerHeight(true) + 20;
            });

            $('#content').height(h);

            if($('#post-content').height() > $('#content').height()){
                $('#content').height($('#post-content').height());
            }

            $('#post-content').toggleClass("active");
        });

        return false;
    });

Thank you all for your comments. I will learn to better organize my code ^_^

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文