admin管理员组

文章数量:1292151

I have used boostrap modal.in that we need to use scroll to specific element. i tried by using below code.but doesn't works

 $('#centralModalLg').on('show.bs.modal', function() {
        $( "#elementId" ).scrollTop(0);

   });

Updated:

when i open modal window at first time and scroll to the specific area.look at the below the screen shot

when open modal second time. the scoll should go the top but i got where i stoped last time.

Html:

<div class="modal fade" id="centralModalLg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog" [ngClass]="{ 'modal-lg': productDetails?.imageUrl != null, 'modal-md': productDetails?.imageUrl == null}" role="document">
        <!--Content-->
        <div class="modal-content" *ngIf="productDetails">
            <div class="modal-body">

                <div class="row">
                    <div class="col-md-12">
                        <button *ngIf="productDetails.imageUrl != null" type="button" id="closeProductModal" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                      </button>

                        <h4>{{productDetails?.productName}}
                            <span>
                              <img *ngIf="!productDetails?.isVeg" alt="trending" class="w24" src=".png">
                            </span>
                            <span>
                                     <img  *ngIf="productDetails?.isVeg" alt="veg" class="w24" src=".png">
                             </span>
                        </h4>
                        <div id="elementId" class="over-flow-md">
                            <img *ngIf="productDetails.imageUrl != null" class="card-img-top" src="{{baseLogoUrl + productDetails?.imageUrl}}" alt="Thumbnail [100%x225]" style="height: 225px; width: 100%; display: block;" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22348%22%20height%3D%22225%22%20xmlns%3D%22http%3A%2F%2Fwww.w3%2F2000%2Fsvg%22%20viewBox%3D%220%200%20348%20225%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_16654384883%20text%20%7B%20fill%3A%23eceeef%3Bfont-weight%3Abold%3Bfont-family%3AArial%2C%20Helvetica%2C%20Open%20Sans%2C%20sans-serif%2C%20monospace%3Bfont-size%3A17pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_16654384883%22%3E%3Crect%20width%3D%22348%22%20height%3D%22225%22%20fill%3D%22%2355595c%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22116.7265625%22%20y%3D%22120.3%22%3EThumbnail%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E"
                                data-holder-rendered="true">

                            <button *ngIf="productDetails.imageUrl == null" type="button" id="closeProductModal" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                              </button>



                            <!-- <hr /> -->

                            <p class="small pt-2">
                                {{ productDetails?.productDesc }}
                            </p>
                            <hr />
                            <div class="addons" *ngFor="let addonCate of productDetails?.addonsCategoryDataList;let addonIndex=index">
                                <h5 class="bg-light">{{addonCate.CategoryType}}</h5>
                                <div class="row pb-1">
                                    <div class="small col-12"><strong>{{addonCate?.addonsCategoryName}}({{addonCate?.addonsCategoryDesc}}) </strong>
                                        <span *ngIf="addonCate.addonType == 'required'" class="float-right pr-2 required">Required</span></div>
                                    <div id="errorElement" *ngIf="addonCate?.hasError" class="small col-12 form-error">{{addonCate?.errorMsg}}</div>

                                </div>
                                <ul class="list-group mb-3" *ngIf="addonCate?.maxNoOfSelection > 1 || addonCate?.maxNoOfSelection == null">
                                    <li class="list-group-item px-0 d-flex justify-content-between lh-condensed" *ngFor="let addonCateList of addonCate?.addonsCategoryEntries;let addoncateIndex=index">
                                        <div class="col-md-7 align-self">
                                            <label class="checkbox-inline checkbox">
                                                    <input class="form-check-input float-left mr-2" name="{{addonCateList.addonsCategoryEntryId}}" (change)="addOnAddRemove(addonIndex,addoncateIndex,addonCateList, $event,'checkbox')" [(ngModel)]="addonCateList.checkedValue"  type="checkbox"  value="option1" >
                                                    <span class="checkmark"></span>
                                                    <div class="checkbox-text">
                                                        <h6 class="my-0">{{addonCateList.addonsCategoryEntryName}}</h6>
                                                    </div>
                                                </label>
                                        </div>
                                        <div class="col-md-2 align-self text-right">
                                            $ {{addonCateList.addonsCategoryEntryPrice}}
                                        </div>
                                        <div class="col-md-3 addprice">
                                            <ng-container *ngIf="addonCateList.maxNoOfSelection > 1 || addonCateList.maxNoOfSelection == null">
                                                <button [disabled]="addonCateList.maxNoOfSelection == addonCateList.quantity" (click)="addonQuantityUpgrade(addonIndex, addoncateIndex, addonCateList, 'add')" class="btn-addRemove float-right" type="button"> <i class="fa fa-plus small"></i> </button>
                                                <input class="form-control input-addRemove float-right" min="{{addonCateList.minNoOfSelection}}" max="{{addonCateList.maxNoOfSelection}}" name="addonCateList_qty" [(ngModel)]="addonCateList.quantity" readonly type="number" aria-label="number" />
                                                <button (click)="addonQuantityUpgrade(addonIndex, addoncateIndex, addonCateList, 'sub')" class="btn-addRemove float-right" type="button"> <i class="fa fa-minus small"></i> </button>


                                                <span id="errorElement" *ngIf="addonCateList.hasError">{{addonCateList.errorMsg}}</span>
                                            </ng-container>
                                        </div>
                                    </li>
                                </ul>
                                <ul class="list-group mb-3" *ngIf="addonCate?.maxNoOfSelection == 1">
                                    <li class="list-group-item pr-0 d-flex justify-content-between lh-condensed" *ngFor="let addonCateList of addonCate?.addonsCategoryEntries;let addoncateIndex=index">
                                        <div class="col-md-7 align-self">
                                            <h6 class="my-0">{{addonCateList.addonsCategoryEntryName}}</h6>
                                            <input class="form-check-input" name="{{addonCate.CategoryType}}" (change)="addOnAddRemove(addonIndex,addoncateIndex,addonCateList, $event,'radio')" [checked]="addonCateList.checkedValue" type="radio">
                                        </div>
                                        <div class="col-md-2 align-self text-right">
                                            $ {{addonCateList.addonsCategoryEntryPrice}}
                                        </div>
                                        <div class="col-md-3 addprice">
                                            <ng-container *ngIf="addonCateList.maxNoOfSelection > 1 || addonCateList.maxNoOfSelection == null">
                                                <button [disabled]="addonCateList.maxNoOfSelection == addonCateList.quantity" (click)="addonQuantityUpgrade(addonIndex, addoncateIndex, addonCateList, 'add')" class="btn-addRemove float-right" type="button"> <i class="fa fa-plus small"></i> </button>
                                                <input class="form-control input-addRemove float-right" min="{{addonCateList.minNoOfSelection}}" max="{{addonCateList.maxNoOfSelection}}" name="addonCateList_qty" [(ngModel)]="addonCateList.quantity" readonly type="number" aria-label="number" />
                                                <button (click)="addonQuantityUpgrade(addonIndex, addoncateIndex, addonCateList, 'sub')" class="btn-addRemove float-right" type="button"> <i class="fa fa-minus small"></i> </button>


                                                <span id="errorElement" *ngIf="addonCateList.hasError">{{addonCateList.errorMsg}}</span>
                                            </ng-container>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>

                </div>
                <div class="py-2">
                    <input class="form-control" name="specialInstr" [(ngModel)]="productDetails.specialInstr" type="text" placeholder="Add Special Instructions for the Restaurant">
                </div>
                <hr />
                <div class="row addprice">
                    <div class="price col-sm-7">Choose Quantity</div>
                    <div class="text-right col-sm-2 ">
                        <strong>{{outletDetials?.currency}}
                            <ng-container *ngIf="productDetails?.cost">{{productDetails.cost}}</ng-container>
                            <ng-container *ngIf="!productDetails?.cost || productDetails.cost == null">0</ng-container>
                        </strong>
                    </div>
                    <div class="text-right  col-sm-3 ">
                        <button class="btn-addRemove float-right" (click)="increase_decrease_qty(productDetails,'add')" type="button"> <i class="fa fa-plus small"></i> </button>
                        <input readonly class="form-control input-addRemove float-right ng-untouched ng-pristine ng-valid" aria-label="number" min="1" name="quantity" [(ngModel)]="productDetails.quantity" type="number">
                        <button class="btn-addRemove float-right" (click)="increase_decrease_qty(productDetails,'sub')" type="button"> <i class="fa fa-minus small"></i> </button>
                    </div>
                </div>

            </div>
            <div class="bg-light p-0 pb-2">
                <div class="row">
                    <div class="col-md-6">
                        <div class="mt-3 pl-3">
                            <span class="pr-1">Total:</span>
                            <strong>{{outletDetials?.currency}}
                                        <ng-container *ngIf="productDetails?.totalPrice">{{productDetails.totalPrice}}</ng-container>
                                        <ng-container *ngIf="!productDetails?.totalPrice || productDetails.totalPrice == null">0</ng-container>
                                    </strong>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <button [disabled]="add_cart_loading == 'adding'" type="button" (click)="addProductToCart(productDetails)" class="btn btn-primary pull-right mt-2 mr-4">
                                    <span *ngIf="add_cart_loading != 'adding'">
                                      <ng-container *ngIf="!update_product_status"> Add to Cart </ng-container>
                                      <ng-container *ngIf="update_product_status">Update</ng-container>
                                    </span>
                                    <span *ngIf="add_cart_loading == 'adding'">
                                      <ng-container *ngIf="!update_product_status">Adding</ng-container>
                                      <ng-container *ngIf="update_product_status">Updating</ng-container>
                                      <i class="fa fa-spinner fa-spin"></i>
                                    </span>
                        </button>
                    </div>
                </div>

            </div>
        </div>
        <!--/.Content-->
    </div>
</div>

How i can achieve this

Thanks in advance

I have used boostrap modal.in that we need to use scroll to specific element. i tried by using below code.but doesn't works

 $('#centralModalLg').on('show.bs.modal', function() {
        $( "#elementId" ).scrollTop(0);

   });

Updated:

when i open modal window at first time and scroll to the specific area.look at the below the screen shot

when open modal second time. the scoll should go the top but i got where i stoped last time.

Html:

<div class="modal fade" id="centralModalLg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog" [ngClass]="{ 'modal-lg': productDetails?.imageUrl != null, 'modal-md': productDetails?.imageUrl == null}" role="document">
        <!--Content-->
        <div class="modal-content" *ngIf="productDetails">
            <div class="modal-body">

                <div class="row">
                    <div class="col-md-12">
                        <button *ngIf="productDetails.imageUrl != null" type="button" id="closeProductModal" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                      </button>

                        <h4>{{productDetails?.productName}}
                            <span>
                              <img *ngIf="!productDetails?.isVeg" alt="trending" class="w24" src="https://www.crashmeal./assets/images/icons/trending-m.png">
                            </span>
                            <span>
                                     <img  *ngIf="productDetails?.isVeg" alt="veg" class="w24" src="https://www.crashmeal./assets/images/icons/veg-m.png">
                             </span>
                        </h4>
                        <div id="elementId" class="over-flow-md">
                            <img *ngIf="productDetails.imageUrl != null" class="card-img-top" src="{{baseLogoUrl + productDetails?.imageUrl}}" alt="Thumbnail [100%x225]" style="height: 225px; width: 100%; display: block;" src="data:image/svg+xml;charset=UTF-8,%3Csvg%20width%3D%22348%22%20height%3D%22225%22%20xmlns%3D%22http%3A%2F%2Fwww.w3%2F2000%2Fsvg%22%20viewBox%3D%220%200%20348%20225%22%20preserveAspectRatio%3D%22none%22%3E%3Cdefs%3E%3Cstyle%20type%3D%22text%2Fcss%22%3E%23holder_16654384883%20text%20%7B%20fill%3A%23eceeef%3Bfont-weight%3Abold%3Bfont-family%3AArial%2C%20Helvetica%2C%20Open%20Sans%2C%20sans-serif%2C%20monospace%3Bfont-size%3A17pt%20%7D%20%3C%2Fstyle%3E%3C%2Fdefs%3E%3Cg%20id%3D%22holder_16654384883%22%3E%3Crect%20width%3D%22348%22%20height%3D%22225%22%20fill%3D%22%2355595c%22%3E%3C%2Frect%3E%3Cg%3E%3Ctext%20x%3D%22116.7265625%22%20y%3D%22120.3%22%3EThumbnail%3C%2Ftext%3E%3C%2Fg%3E%3C%2Fg%3E%3C%2Fsvg%3E"
                                data-holder-rendered="true">

                            <button *ngIf="productDetails.imageUrl == null" type="button" id="closeProductModal" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                              </button>



                            <!-- <hr /> -->

                            <p class="small pt-2">
                                {{ productDetails?.productDesc }}
                            </p>
                            <hr />
                            <div class="addons" *ngFor="let addonCate of productDetails?.addonsCategoryDataList;let addonIndex=index">
                                <h5 class="bg-light">{{addonCate.CategoryType}}</h5>
                                <div class="row pb-1">
                                    <div class="small col-12"><strong>{{addonCate?.addonsCategoryName}}({{addonCate?.addonsCategoryDesc}}) </strong>
                                        <span *ngIf="addonCate.addonType == 'required'" class="float-right pr-2 required">Required</span></div>
                                    <div id="errorElement" *ngIf="addonCate?.hasError" class="small col-12 form-error">{{addonCate?.errorMsg}}</div>

                                </div>
                                <ul class="list-group mb-3" *ngIf="addonCate?.maxNoOfSelection > 1 || addonCate?.maxNoOfSelection == null">
                                    <li class="list-group-item px-0 d-flex justify-content-between lh-condensed" *ngFor="let addonCateList of addonCate?.addonsCategoryEntries;let addoncateIndex=index">
                                        <div class="col-md-7 align-self">
                                            <label class="checkbox-inline checkbox">
                                                    <input class="form-check-input float-left mr-2" name="{{addonCateList.addonsCategoryEntryId}}" (change)="addOnAddRemove(addonIndex,addoncateIndex,addonCateList, $event,'checkbox')" [(ngModel)]="addonCateList.checkedValue"  type="checkbox"  value="option1" >
                                                    <span class="checkmark"></span>
                                                    <div class="checkbox-text">
                                                        <h6 class="my-0">{{addonCateList.addonsCategoryEntryName}}</h6>
                                                    </div>
                                                </label>
                                        </div>
                                        <div class="col-md-2 align-self text-right">
                                            $ {{addonCateList.addonsCategoryEntryPrice}}
                                        </div>
                                        <div class="col-md-3 addprice">
                                            <ng-container *ngIf="addonCateList.maxNoOfSelection > 1 || addonCateList.maxNoOfSelection == null">
                                                <button [disabled]="addonCateList.maxNoOfSelection == addonCateList.quantity" (click)="addonQuantityUpgrade(addonIndex, addoncateIndex, addonCateList, 'add')" class="btn-addRemove float-right" type="button"> <i class="fa fa-plus small"></i> </button>
                                                <input class="form-control input-addRemove float-right" min="{{addonCateList.minNoOfSelection}}" max="{{addonCateList.maxNoOfSelection}}" name="addonCateList_qty" [(ngModel)]="addonCateList.quantity" readonly type="number" aria-label="number" />
                                                <button (click)="addonQuantityUpgrade(addonIndex, addoncateIndex, addonCateList, 'sub')" class="btn-addRemove float-right" type="button"> <i class="fa fa-minus small"></i> </button>


                                                <span id="errorElement" *ngIf="addonCateList.hasError">{{addonCateList.errorMsg}}</span>
                                            </ng-container>
                                        </div>
                                    </li>
                                </ul>
                                <ul class="list-group mb-3" *ngIf="addonCate?.maxNoOfSelection == 1">
                                    <li class="list-group-item pr-0 d-flex justify-content-between lh-condensed" *ngFor="let addonCateList of addonCate?.addonsCategoryEntries;let addoncateIndex=index">
                                        <div class="col-md-7 align-self">
                                            <h6 class="my-0">{{addonCateList.addonsCategoryEntryName}}</h6>
                                            <input class="form-check-input" name="{{addonCate.CategoryType}}" (change)="addOnAddRemove(addonIndex,addoncateIndex,addonCateList, $event,'radio')" [checked]="addonCateList.checkedValue" type="radio">
                                        </div>
                                        <div class="col-md-2 align-self text-right">
                                            $ {{addonCateList.addonsCategoryEntryPrice}}
                                        </div>
                                        <div class="col-md-3 addprice">
                                            <ng-container *ngIf="addonCateList.maxNoOfSelection > 1 || addonCateList.maxNoOfSelection == null">
                                                <button [disabled]="addonCateList.maxNoOfSelection == addonCateList.quantity" (click)="addonQuantityUpgrade(addonIndex, addoncateIndex, addonCateList, 'add')" class="btn-addRemove float-right" type="button"> <i class="fa fa-plus small"></i> </button>
                                                <input class="form-control input-addRemove float-right" min="{{addonCateList.minNoOfSelection}}" max="{{addonCateList.maxNoOfSelection}}" name="addonCateList_qty" [(ngModel)]="addonCateList.quantity" readonly type="number" aria-label="number" />
                                                <button (click)="addonQuantityUpgrade(addonIndex, addoncateIndex, addonCateList, 'sub')" class="btn-addRemove float-right" type="button"> <i class="fa fa-minus small"></i> </button>


                                                <span id="errorElement" *ngIf="addonCateList.hasError">{{addonCateList.errorMsg}}</span>
                                            </ng-container>
                                        </div>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>

                </div>
                <div class="py-2">
                    <input class="form-control" name="specialInstr" [(ngModel)]="productDetails.specialInstr" type="text" placeholder="Add Special Instructions for the Restaurant">
                </div>
                <hr />
                <div class="row addprice">
                    <div class="price col-sm-7">Choose Quantity</div>
                    <div class="text-right col-sm-2 ">
                        <strong>{{outletDetials?.currency}}
                            <ng-container *ngIf="productDetails?.cost">{{productDetails.cost}}</ng-container>
                            <ng-container *ngIf="!productDetails?.cost || productDetails.cost == null">0</ng-container>
                        </strong>
                    </div>
                    <div class="text-right  col-sm-3 ">
                        <button class="btn-addRemove float-right" (click)="increase_decrease_qty(productDetails,'add')" type="button"> <i class="fa fa-plus small"></i> </button>
                        <input readonly class="form-control input-addRemove float-right ng-untouched ng-pristine ng-valid" aria-label="number" min="1" name="quantity" [(ngModel)]="productDetails.quantity" type="number">
                        <button class="btn-addRemove float-right" (click)="increase_decrease_qty(productDetails,'sub')" type="button"> <i class="fa fa-minus small"></i> </button>
                    </div>
                </div>

            </div>
            <div class="bg-light p-0 pb-2">
                <div class="row">
                    <div class="col-md-6">
                        <div class="mt-3 pl-3">
                            <span class="pr-1">Total:</span>
                            <strong>{{outletDetials?.currency}}
                                        <ng-container *ngIf="productDetails?.totalPrice">{{productDetails.totalPrice}}</ng-container>
                                        <ng-container *ngIf="!productDetails?.totalPrice || productDetails.totalPrice == null">0</ng-container>
                                    </strong>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <button [disabled]="add_cart_loading == 'adding'" type="button" (click)="addProductToCart(productDetails)" class="btn btn-primary pull-right mt-2 mr-4">
                                    <span *ngIf="add_cart_loading != 'adding'">
                                      <ng-container *ngIf="!update_product_status"> Add to Cart </ng-container>
                                      <ng-container *ngIf="update_product_status">Update</ng-container>
                                    </span>
                                    <span *ngIf="add_cart_loading == 'adding'">
                                      <ng-container *ngIf="!update_product_status">Adding</ng-container>
                                      <ng-container *ngIf="update_product_status">Updating</ng-container>
                                      <i class="fa fa-spinner fa-spin"></i>
                                    </span>
                        </button>
                    </div>
                </div>

            </div>
        </div>
        <!--/.Content-->
    </div>
</div>

How i can achieve this

Thanks in advance

Share Improve this question edited Jan 11, 2019 at 11:20 Robert asked Jan 7, 2019 at 12:22 RobertRobert 3,4831 gold badge20 silver badges36 bronze badges 16
  • $('#centralModalLg').on('.show.bs.modal', function() { $( "#elementId" ).scrollTop(0); }); Are you missing #elementId and .show.bs.modal from above code – Ayush Sharma Commented Jan 7, 2019 at 12:24
  • sorry that was type error. i have tried this way only "$( "#elementId" ).scrollTop(0);" – Robert Commented Jan 7, 2019 at 12:40
  • 2 Can you please add html and show us what exactly you want. – Just code Commented Jan 9, 2019 at 12:52
  • Add your html code – Udara Kasun Commented Jan 9, 2019 at 13:04
  • @UdaraKasun updated – Robert Commented Jan 9, 2019 at 13:08
 |  Show 11 more ments

3 Answers 3

Reset to default 6 +25

Change "show" to "shown" when did you use bootstrap 3 or 4

$('#centralModalLg').on('shown.bs.modal', function() {
        $( "#elementId" ).scrollTop(0);
});

check that Bootstrap 3 codepen example

check that Bootstrap 4 codepen example

$('#centralModalLg').on('shown.bs.modal', function() {
    $('#elementId').scrollTop(0); 
});

A Fiddle with your code.

EDIT: The issue is caused by the event listener, should be shown.bs.modal instead of show.bs.modal, updated the fiddle as well.

jQuery.scrollTop() could be conflicting if the event is started but not yet pleted as of Bootstrap's official documentation :

the infinitive (ex. show) is triggered at the start of an event, and its past participle form (ex. shown) is triggered on the pletion of an action.

try this:

$('#centralModalLg').on('show.bs.modal', function() {

    setTimeout(function() {
    var $elem = $('#scrollto') // add this ID to an element you want scroll to
    var $container = $("#elementId")
    $container.scrollTop(0) // edge case - reset scroll in case it is reopened
    var t = $container.offset().top
    $container.scrollTop($elem.position().top - t);
    }, 500) // run after modal animation 
});

It is based on this answer: https://stackoverflow./a/21179878/8164758

Here is working jsfiddle example: https://jsfiddle/954jnt2y/1/

本文标签: javascriptInside bootstrap modal scroll to specific element is not workingStack Overflow