【问题标题】:Alert when div height changesdiv 高度变化时发出警报
【发布时间】:2012-08-05 12:25:42
【问题描述】:

我整天都在寻找这个,但我自己无法弄清楚..

我有一个购物车,您可以在其中添加商品。购物车位于下拉列表中,因此您必须单击它才能查看它。因此,每次您将商品添加到购物车时,我都想在某处显示“+1”、“+2”等,当您单击下拉菜单时,它会消失,因此它可以重新开始计数。 所以,我认为当 div 的高度发生变化时,它可以显示 +1。

但是,我不知道足够的 javascript 来做到这一点......

我的html:

<div id="button">Button</div>
  <div id="chartdropupcontainer">
    <div id="chart">
      <button>test</button>
<script type="text/javascript">
$("button").click(function () {
    $("#testp").hide();
});    

$("#chart").bind("resize", function(){ 
    alert("test"); 
});
</script>
      <p id="testp"></p>
    </div>
  </div>
</div>

我的 CSS:

* {
 padding: 0;
 margin: 0;
 font-family: Helvetica;
 color: white;
}

#chartdropupcontainer {
 width: 200px;
 background-color: red;
 position: fixed;
 bottom: 0;
 right: 0;
 margin-right: 20px;
 padding: 0 5px 0 5px;
 margin-bottom: 47px;
 z-index: 998;
}
#chartdropupcontainer h1 {
 margin: 5px;
 color: black;
 cursor: pointer;
}
#chart {
 width: 100%;
 background-color: black;
 height: 400px;
 overflow: auto;

}
#button {
 background-color: blue;
 cursor: pointer;
 padding: 10px;
 width: 190px;
 position: fixed;
 bottom: 0;
 right: 0;
 margin-right: 20px;
 z-index: 999;
}

我的javascript:

$(document).ready(function() {
    $("#button").click(function () {
      $("#chartdropupcontainer").slideToggle("slow");
    });
});

这里是一个在线版本的链接:http://www.rutgerinc.nl/niels/

编辑:对不起,有点不礼貌! 谁能帮我解决这个问题?

【问题讨论】:

  • 你不能把它挂在添加商品到购物车的方法中吗?或者让购物车对象在添加商品时触发一个事件。让它依赖于 html 标签的维度属性似乎有点奇怪和不稳定。
  • 我真的不会依赖表示更改来表示应用程序状态的更改。例如,如果用户增加了字体大小,您可能会看到触发了相同的事件。我真的建议要么将此业务逻辑拉入您的服务器脚本(您可能更舒服)或构建一些代码来处理用户交互。购物车是您不想在其中出错的应用程序。

标签: javascript jquery css html height


【解决方案1】:

首先,resize 事件发生在您调整窗口大小时,这就是您的事件永远不会触发的原因。 http://api.jquery.com/resize/

我认为您需要更改将项目添加到购物篮的代码。是不是有更多的 javascript 可以在一开始就将东西添加到购物篮中?

像 BGerrissen 建议的事件驱动方法将是完美的,因为您可以在用户向购物车添加东西时触发一个或多个独立函数。

当您使用 jQuery 时,自定义事件非常简单。您使用触发器触发事件​​,然后绑定以侦听它。 http://api.jquery.com/trigger/

【讨论】:

  • hmm ... jquery 触发器似乎有点方便,可能会这样做,但我仍然没有他的知识来写整个事情...这不是我懒惰或任何东西,我只是不知道 javascript...
猜你喜欢
  • 2019-10-09
  • 2015-01-17
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-03
  • 1970-01-01
  • 2022-01-14
相关资源
最近更新 更多