【问题标题】:jQuery: Div elements are not showing upjQuery:Div 元素没有出现
【发布时间】:2010-05-25 12:02:33
【问题描述】:

我正在调整 Coverflow technique 以使用 div。以下是html:

    <html> 
 <head> 
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
  <style type="text/css" media="screen"> 
   body,html {
    margin: 0;
    padding: 0;
    background: #000;
    height: 100%;
    color: #eee;
    font-family: Arial;
    font-size: 10px;
   }
   div.magnifyme {
    height: 80px;
    padding: 80px;
    position: absolute;
    top: 0px;
    left: 0px;
    width: 2000px;
   }
   div.wrapper {
    margin: 0px;
    height: 470px;
    /*border: 2px solid #999;*/
    overflow: hidden;
    padding-left: 40px;
    right: 1px;
    width: 824px;
    position: relative;
   }

   div.container {position: relative; width: 854px; height: 480px; background: #000; margin: auto;}
   div.nav {position: absolute; top: 10px; width: 20%; height: 10%; right: 1px; }
   div.magnifyme div {
    position: absolute;
    width: 300px;
    height: 280px;
    float: left;
    margin: 5px;
    position: relative;
    border: 2px solid #999;
    background: #500;
   }
  </style> 
  <script type="text/javascript" src="jquery-1.3.2.js"></script> 
  <script type="text/javascript" src="ui.coverflow.js"></script> 
  <script type="text/javascript" src="ui.core.js"></script>  
  <script type="text/javascript"> 
   $(function() {
              $("div.magnifyme").coverflow();
    $("#add").click(function() {
     $(".magnifyme").append("<div id=\"div5\">hello world</div>");
     $("div.magnifyme").coverflow();
    });

   });

  </script> 
 </head> 
 <body> 
    <div class="container">
    <div class="wrapper"> 
  <div class="magnifyme"> 
   <div id="div0">This is div 0</div>  
   <div id="div1">This is div 1</div>  
   <div id="div2">This is div 2</div>  
   <div id="div3">This is div 3</div>  
   <div id="div4">This is div 4</div>  

  </div> 
    </div>
    <div class="nav">
  <button type="button" id="add">Add to Deck</button>
    </div> 
   </div>
</body> 
</html>  

coverflow 函数(作为 js 文件包含在 head 部分中)是 here。当我单击该按钮时,我希望它将 DIV 添加到已经存在的卡组中。由于某种原因,它没有显示新添加的 DIV。添加新元素后,我尝试调用 coverflow() 函数,但这也不起作用。修改后的coverflow函数在这里给出:

;(function($){

    $.widget("ui.coverflow", {
        init: function() {

            var self = this;

            this.items = $(this.options.items, this.element).bind("click", function() {
                self.moveTo(this);
                //$("div.slider").slider("moveTo", self.current, null, true);
            });
            this.itemWidth = this.items.outerWidth(true);

            this.current = 0; //Start item

            this.refresh(1, 0, this.current);
            this.element.css("left",
                (-this.current * this.itemWidth/2)
                + (this.element.parent()[0].offsetWidth/2 - this.itemWidth/2) //Center the items container
                - (parseInt(this.element.css("paddingLeft")) || 0) //Subtract the padding of the items container
            );

        },
        moveTo: function(item) {

            this.previous = this.current;
            this.current = !isNaN(parseInt(item)) ? parseInt(item) : this.items.index(item);
            if(this.previous == this.current) return false; //Don't animate when clicking on the same item

            var self = this, to = Math.abs(self.previous-self.current) <=1 ? self.previous : self.current+(self.previous < self.current ? -1 : 1);
            $.fx.step.coverflow = function(fx) {
                self.refresh(fx.now, to, self.current);
            };

            this.element.stop().animate({
                coverflow: 1,
                left: (
                    (-this.current * this.itemWidth/2)
                    + (this.element.parent()[0].offsetWidth/2 - this.itemWidth/2) //Center the items container
                    - (parseInt(this.element.css("paddingLeft")) || 0) //Subtract the padding of the items container
                )
            }, {
                duration: 1000,
                easing: "easeOutQuint"
            });

            /*current = this.current;
            $("[id^=div]").each(function() {
                if(this.id != "div"+current) {
                    console.info(this.id + " Current: " + current);
                    $(this).fadeTo( 'slow', 0.1);
                }
            });*/

        },
        refresh: function(state,from,to) {

            var self = this, offset = null;

            this.items.each(function(i) {

                var side = (i == to && from-to < 0 ) ||  i-to > 0 ? "left" : "right";
                var mod = i == to ? (1-state) : ( i == from ? state : 1 );              

                var before = (i > from && i != to);

                $(this).css({
                    webkitTransform: "matrix(1,"+(mod * (side == "right" ? -0.5 : 0.5))+",0,1,0,0) scale("+(1+((1-mod)*0.5))+")",
                    left: (
                        (-i * (self.itemWidth/2))
                        + (side == "right"? -self.itemWidth/2 : self.itemWidth/2) * mod //For the space in the middle
                    ),
                    zIndex: self.items.length + (side == "left" ? to-i : i-to)
                });

                if(!$.browser.msie)
                    $(this).css("opacity", 1 - Math.abs((side == "left" ? to-i : i-to))/2);
            });

        }
    });

    $.extend($.ui.coverflow, {
        defaults: {
            items: "> *"
        }
    });

})(jQuery); 

我注意到的一件事是,在单击按钮大约 5-10 次后,元素会显示出来,但不是与已经存在的 div 一起出现,而是在它们下方。我猜这与 magnifyme 类(2000px)的 CSS 有关,但我不确定它是什么。有什么办法可以使这项工作?

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    您需要为coverflow 小部件编写一个附加函数:

        add: function(el) {
            var self = this;
            this.element.append(el)
            this.options.items = $('> *', this.element);
            this.items = $(this.options.items, this.element).bind("click", function() {
                self.moveTo(this);
            });
            this.itemWidth = this.items.outerWidth(true);
            this.moveTo(this.items.length-1);
        },
    

    然后这样称呼它:

    $("#add").click(function() {
        $("div.magnifyme").coverflow('add', "<div></div>");
    });
    

    【讨论】:

    • 太棒了...非常感谢!完美运行...非常感谢您的时间。
    【解决方案2】:

    首先,您需要添加对 jQuery UI 核心的引用,而且它似乎也需要 jQuery 滑块插件。

    其次,在您的点击事件中,您正在执行 location.reload,它正在从服务器刷新页面,重置您对页面所做的任何更改。 (如果你让 DIV 变得更小,你可以在页面重新加载之前看到一个闪烁)。

    【讨论】:

    • 对不起!那个 location.reload 不知何故出现了。我只是在玩弄它。我刚刚用实际版本更新了我的代码。 +1 纠正我。
    • 你还需要在jQuery滑块库中添加jqueryui.com/demos/slider
    • 谢谢。我正在尝试。同时,我添加了我正在使用的coverflow(我修改了原始的,删除了一些我不需要的东西)。
    • 对不起。我仍然面临同样的问题。 div 被添加,我可以在多次单击按钮后看到它溢出,但它并没有以让我动画它的方式添加。
    【解决方案3】:

    您在页面上遇到 js 错误——“$.widget 不是函数”,因为您没有包含 jqueryUI 库。 http://jqueryui.com/

    此外,如果您删除 location.reload 行,您的代码将起作用,但是,我会像这样重写该脚本块,以便在文档准备好时一切都清楚地运行:

    <script type="text/javascript">
        $(document).ready(function() {
            $("div.magnifyme").coverflow();
            $("#add").click(function() {
                $(".magnifyme").append("<div id=\"div5\">hello world</div>");
                $("div.magnifyme").coverflow();
            });
        });
    </script> 
    

    【讨论】:

    • @user126272:感谢您的回复,但不应该出现 $ 错误,因为我包含了 jQuery 库 :) 就像我在其中一个 cmets 中提到的那样,我刚刚更新了我的代码。该 location.reload 被意外放置。对此感到抱歉。
    • 除了核心 jquery 库之外,您还需要包含 jqueryUI 库。请参阅我上面的链接。 :)
    • 是的。我做到了... jquery-1.3.2.js 行正是这样做的。
    • 哦,无法从您的代码中看出这一点。我不确定问题出在哪里,因为您的代码在单击按钮时确实添加了额外的 div。您看不到它们,因为 .wrapper div 设置为 824px 宽并且溢出:隐藏,因此它们显示在可视区域之外。一旦 div 的行长超过 .magnifyme (2000px) 的宽度,它们就会开始显示在下一行,这就是为什么如果你多次单击它们会显示在下方。
    • 是的...我被困在同一点上。我也尝试再次调用coverflow函数,但由于某种原因它仍然没有出现。
    猜你喜欢
    • 2020-11-16
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2019-05-02
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多