繁体   English   中英

jQuery:Div元素未显示

[英]jQuery: Div elements are not showing up

我正在调整Coverflow技术以与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文件中)在此处 当我单击按钮时,我希望它可以将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有关,但是我不确定它是什么。 有什么办法可以使这项工作吗?

您需要为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>");
});

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

其次,在您的点击事件中,您正在执行location.reload,它正在从服务器刷新页面,并重置对页面所做的任何更改。 (如果您将DIV缩小得多,则在重新加载页面之前,您会看到一闪)。

您在页面上遇到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> 

暂无
暂无

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

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