gpt4 book ai didi

web - 铁页 : Page Changed Event

转载 作者:行者123 更新时间:2023-12-04 14:31:51 26 4
gpt4 key购买 nike

是否有任何事件可以在页面更改时被 Web 组件捕获,甚至是生命周期回调?

我尝试使用附加的回调,但它不会再次被触发..

最佳答案

  • <iron-pages> 的父元素,您可以将 observe 更改为 <iron-pages>.selected 以监视页面索引/名称:

    <head>
    <base href="https://polygit.org/polymer+1.9.3/components/">
    <script src="webcomponentsjs/webcomponents-lite.js"></script>
    <link rel="import" href="polymer/polymer.html">
    <link rel="import" href="paper-button/paper-button.html">
    <link rel="import" href="iron-pages/iron-pages.html">
    </head>
    <body>
    <x-foo></x-foo>

    <dom-module id="x-foo">
    <template>
    <iron-pages id="pages" selected="{{selected}}">
    <div>One</div>
    <div>Two</div>
    <div>Three</div>
    </iron-pages>
    <paper-button on-tap="_prev">Prev</paper-button>
    <paper-button on-tap="_next">Next</paper-button>
    </template>
    <script>
    HTMLImports.whenReady(function() {
    Polymer({
    is: 'x-foo',
    properties : {
    selected: {
    type: Number,
    value: 0,
    observer: '_selectedChanged'
    }
    },
    _selectedChanged: function(newPage, oldPage) {
    console.log('<iron-pages>.selected', 'new', newPage, 'old', oldPage);
    },
    _prev: function() {
    this.$.pages.selectPrevious();
    },
    _next: function() {
    this.$.pages.selectNext();
    }
    });
    });
    </script>
    </dom-module>
    </body>

    codepen
  • 或者您可以为 event listener <iron-pages>.iron-select 事件设置一个 <iron-pages>.iron-deselect 以查看选定和取消选定的元素。

    <head>
    <base href="https://polygit.org/polymer+1.9.3/components/">
    <script src="webcomponentsjs/webcomponents-lite.js"></script>
    <link rel="import" href="polymer/polymer.html">
    <link rel="import" href="paper-button/paper-button.html">
    <link rel="import" href="iron-pages/iron-pages.html">
    </head>
    <body>
    <x-foo></x-foo>

    <dom-module id="x-foo">
    <template>
    <iron-pages id="pages" selected="0"
    on-iron-select="_pageSelected"
    on-iron-deselect="_pageDeselected">
    <div>One</div>
    <div>Two</div>
    <div>Three</div>
    </iron-pages>
    <paper-button on-tap="_prev">Prev</paper-button>
    <paper-button on-tap="_next">Next</paper-button>
    </template>
    <script>
    HTMLImports.whenReady(function() {
    Polymer({
    is: 'x-foo',
    _pageSelected: function(e) {
    var page = e.detail.item;
    console.log('page selected', page);
    },
    _pageDeselected: function(e) {
    var page = e.detail.item;
    console.log('page deselected', page);
    },
    _prev: function() {
    this.$.pages.selectPrevious();
    },
    _next: function() {
    this.$.pages.selectNext();
    }
    });
    });
    </script>
    </dom-module>
    </body>

    codepen
  • 或者您可以配置 <iron-pages>.selectedAttribute 以便它在新的和以前选择的页面上设置一个属性,您可以从页面本身中观察到。当页面选择发生变化时,之前选择的页面的属性设置为 false ,新选择的页面的属性设置为 true

    <head>
    <base href="https://polygit.org/polymer+1.9.3/components/">
    <script src="webcomponentsjs/webcomponents-lite.js"></script>
    <link rel="import" href="polymer/polymer.html">
    <link rel="import" href="paper-button/paper-button.html">
    <link rel="import" href="iron-pages/iron-pages.html">
    </head>
    <body>
    <x-foo></x-foo>

    <dom-module id="x-foo">
    <template>
    <iron-pages id="pages" selected="0" selected-attribute="selected">
    <x-page data-name="p1">One</x-page>
    <x-page data-name="p2">Two</x-page>
    <x-page data-name="p3">Three</x-page>
    </iron-pages>
    <paper-button on-tap="_prev">Prev</paper-button>
    <paper-button on-tap="_next">Next</paper-button>
    </template>
    <script>
    HTMLImports.whenReady(function() {
    Polymer({
    is: 'x-foo',
    _prev: function() {
    this.$.pages.selectPrevious();
    },
    _next: function() {
    this.$.pages.selectNext();
    }
    });
    });
    </script>
    </dom-module>

    <dom-module id="x-page">
    <template>
    <content id="content"></content>
    </template>
    <script>
    HTMLImports.whenReady(function() {
    Polymer({
    is: 'x-page',
    properties: {
    selected: {
    type: Boolean,
    value: false,
    observer: '_selectedChanged'
    }
    },
    _selectedChanged: function(selected) {
    console.log('<x-page>.sel', this.dataset.name, selected);
    }
    });
    });
    </script>
    </dom-module>
    </body>

    codepen
  • 关于web - 铁页 : Page Changed Event,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39954560/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com