2016-09-08 9 views
5

पर क्लिक करें, उदाहरण के लिए, जब मैं बटन पर क्लिक करता हूं तो Ionic2 के भीतर scrollIntoView() के समान कुछ ढूंढ रहा हूं।जब यह क्लिक किया गया है तो तत्व को स्क्रॉल करने के लिए

ion-content सहायता में विस्तृत विधियों में से कोई भी नहीं।

उत्तर

8

कृपया this working plunker

पर एक नज़र डालें आप scrollTo(x,y,duration) विधि (docs) उपयोग कर सकते हैं। कोड बहुत सरल है, पहले हम लक्ष्य तत्व की स्थिति को (इस मामले में एक <p></p>) प्राप्त कर रहा है और फिर हम का उपयोग scrollTo(...) विधि में है कि। पहला दृश्य:

<ion-header> 
    <ion-navbar primary> 
    <ion-title> 
     <span>My App</span> 
    </ion-title> 
    </ion-navbar> 
</ion-header> 

<ion-content> 

    <button ion-button text-only (click)="scrollElement()">Click me</button> 

    <div style="height: 600px;"></div> 

    <!-- Notice the #target in the p element --> 
    <p #target>Secret message!</p> 

    <div style="height: 600px;"></div> 

</ion-content> 

और घटक कोड:

import { ViewChild, Component } from '@angular/core'; 
import { NavController, Content } from 'ionic-angular'; 

@Component({ 
    templateUrl:"home.html" 
}) 
export class HomePage { 
    @ViewChild(Content) content: Content; 
    @ViewChild('target') target: any; 

    constructor() { } 

    public scrollElement() { 
    // Avoid reading the DOM directly, by using ViewChild and the target reference 
    this.content.scrollTo(0, this.target.nativeElement.offsetTop, 500); 
    } 
} 
+1

plunker नहीं करता है के लिए लिंक एक सुविधाजनक प्लगइन लिखा था 'टी जवाब से संबंधित कुछ भी प्रदर्शित करने के लिए लग रहे हैं। – Willwsharp

+1

मैं माफी चाहता @Willwsharp हूँ, plunker आयोनिक के एक पुराने संस्करण का इस्तेमाल किया। मैं बाद में आज :) – sebaferreras

+1

@Willwsharp मैं जवाब नवीनीकृत किया है सीधे पढ़ डोम से बचने के लिए एक नया plunker पैदा हो जाएगी। अब यह HTML तत्व – sebaferreras

1

मैं HTML में एक अंकर लिंक का उपयोग करें चाहते हैं।

बस एक

उदाहरण में elemnt और आईडी = "scrollXYZ" बटन देने के लिए और लपेट:

<a href="#scrollXYZ"><button>Example</button></a> 
<div id="scrollXYZ"><h2>Scroll to this</h2></div> 
+3

इस के संदर्भ पाने के लिए 'ViewChild' का उपयोग करता है, तो तत्व @Mackelito कहीं भी होगी गतिशील – Mackelito

+0

जोड़ा जाता है अगर आप scrollTo गतिशील रूप से भी जोड़ काम नहीं करेगा। –

0

मैंने देखा ऊपर समाधान कोणीय यूनिवर्सल सर्वर साइड प्रतिपादन के साथ अच्छी तरह से काम नहीं करते (एसएसआर) document की वजह से उपलब्ध सर्वर साइड नहीं किया जा रहा।

इसलिए मैं कोणीय 4+ में तत्वों SSR

कि AoT साथ काम करते हैं और लोगों को स्क्रॉल प्राप्त करने के लिए NPM
ngx-scroll-to

GitHub
ngx-scroll-to

संबंधित मुद्दे

 संबंधित मुद्दे