简体   繁体   English

如何在jQuery中单击按钮时显示相对于按钮位置的div

[英]How to show a div relative to the position of a button on click of that button in jQuery

Here is my code: 这是我的代码:

$j(".btn-cart").on("click", function( e ){
            e.preventDefault();
            var top = $(this).offset().top;
            alert(top);
            $j("#content").animate({ 
            top: top
            }, 600);

        }

HTML: HTML:

for the div that is to be shown: 对于要显示的div:

 <div id="angularJsApp" title="Basket" ng-app="cart" ng-controller="CartFormController">
        <div id="content" class="draggable" style="display:none;" ng-show="invoice.items.length > 0">
              <input type="checkbox" name="check" id="check"/>
              <label for="check">
              <div id="heads" >
                 <span id="commentBubble">
                   <a href=""><apex:image value="{!URLFOR($Resource.CartStyleBundle,'Shopping-Cart-Button.png')}" alt="" /></a>
                 </span>
              </div>

              <article class="pane" id="user_1">

                <table class="table table-checkout">
                    <tr>

                        <th>Description</th>
                        <th>Qty</th>
                        <th>Cost</th>
                        <th>Total</th>
                        <th></th>
                    </tr>
                    <tr ng-repeat="item in invoice.items">
                        <td>{{item.description}}</td>           
                        <td>{{item.qty}}</td>
                        <td>{{item.cost}}</td>
                        <td>{{item.qty * item.cost | currency : "£" }}</td>
                        <td>
                            [<a href="" ng-click="removeItem($index)" style="cursor: pointer;">X</a>]
                        </td>
                    </tr>
                     <tr>
                        <td></td>
                        <td>Total:</td>
                        <td>{{total() | currency : "£" }}</td>

                    </tr> 
                </table>


            </article>

              </label>
            </div> 
        </div>

And HTML for the buttons: 和HTML的按钮:

<apex:outputPanel id="basketPanel" layout="none">                            
                        <a href="#content" class="btn-cart">
                            <apex:commandButton styleClass="btn btn-default btn-buy" onClick="microappscope().addItem('{!p.name}',{!p.Recommended_Retail_Price__c});" value="Add to Basket" id="btn" rerender="btn" action="{!addProductToCart}" >
                                <apex:param name="pId" value="{!p.Id}" assignTo="{!selectedProductId}"/>
                            </apex:commandButton>
                        </a>
                    </apex:outputPanel>

I want to show the div with id #content which is hidden by default on click of a button class btn-cart . 我想显示ID为#content的div,默认情况下,单击按钮类btn-cart其隐藏。 And the div should open on top of the button. 并且div应该在按钮顶部打开。 The problem is that I have many buttons in the page and on click of each of the button the same div will open relative to the position of the button that is being clicked. 问题是我在页面中有许多按钮,并且在单击每个按钮时,相对于被单击按钮的位置,将打开相同的div。

See screenshot attached: 请参阅所附的屏幕截图: 在此处输入图片说明

You can find the current position (relatve to the document) of the clicked button by using .offset() and then change your div's offset according to that. 您可以使用.offset()找到被单击按钮的当前位置(与文档.offset() ,然后根据该位置更改div的偏移量。

$('button').click(function(){
   var pos = $(this).offset();
    $('#toShow').show();
    $('#toShow').offset( pos );

}); 


Here is small example: http://jsfiddle.net/zje0uu9y/1/ 这是一个小示例: http : //jsfiddle.net/zje0uu9y/1/

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

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