Vue.js如何将滚动位置设置回顶部?

时间:2019-03-20 00:30:05

标签: vue.js scrollview

因此,每当我单击“编辑”按钮时,都会弹出一个带有滚动条的对话框,我可以在其中填写信息。但是,当我单击“取消”或“保存”,然后单击相同的“编辑”按钮时,对话框将在相同的滚动位置弹出。我想每次单击“编辑”并打开对话框时,始终位于对话框页面的顶部,而不是我上次退出的位置。

<template>
    <!-- <div class="text-xs-center" v-if="storeState.admin" lazy> --> 
      <v-dialog 
      transition="dialog-bottom-transition" 
      scrollable 
      fullscreen 
      v-model="sheet" 
      v-if="storeState.admin"  
      lazy 
      persistent 
    > 
      <template v-slot:activator="{on}"> 
        <v-btn flat color="green" dark icon v-on="on"> 
            <v-icon>edit</v-icon>
        </v-btn>
      </template>
        <div background-color="transparent" style="margin: auto auto 0 auto"> 
          <v-card px-5 max-width="800px"> 
            <v-card-title>
                ADD SCHOLARSHIP
            </v-card-title>

        <v-form v-model="addDisabled" validation ref="editForm">
            <v-container>
            <v-layout wrap>
                <v-flex
                xs12
                md4
                >
                <v-text-field
                    v-model="scholarship.title"
                    label="Scholarship name"
                    :counter="maxLength"
                    :rules="[maxLength_rules.max, minLength_rules.min]"
                    required
                ></v-text-field>
                </v-flex>

                <v-flex xs12 md4>
                <v-text-field
                    v-model="scholarship.faculty"
                    label="Faculty"
                    :counter="maxLength"
                    :rules="[maxLength_rules.max, minLength_rules.min]"
                    required
                ></v-text-field>
                </v-flex>

                <v-flex xs12 md4>
                <v-text-field
                    v-model="scholarship.dollarAmount"
                    label="Award amount"
                    required
                    :rules="[amount_rules.range, minLength_rules.min]"
                ></v-text-field>
                </v-flex>

           <v-flex ml-2 xs12 sm6 md4>
            <v-menu
              ref="menu2"
              v-model="menu2"
              :close-on-content-click="false"
              :nudge-right="40"
              :return-value.sync="availableDate"
              transition="scale-transition"
              offset-y
              full-width
              min-width="290px"
            >
              <template v-slot:activator="{ on }">
                <v-text-field
                  v-model="scholarship.available"
                  label="Date Available"
                  prepend-icon="event"
                  :rules="[minLength_rules.min]"
                  readonly
                  v-on="on"
                ></v-text-field>
              </template>
              <v-date-picker v-model="availableDate" no-title scrollable>
                <v-spacer></v-spacer>
                <v-btn flat color="primary" @click="menu2 = false">Cancel</v-btn>
                <v-btn flat color="primary" @click="$refs.menu2.save(availableDate)">OK</v-btn>
              </v-date-picker>
            </v-menu>
          </v-flex>


          <v-flex ml-2 xs12 sm6 md4>
            <v-menu
              ref="menu1"
              v-model="menu1"
              :close-on-content-click="false"
              :nudge-right="40"
              :return-value.sync="dueDate"

              transition="scale-transition"
              offset-y
              full-width
              min-width="290px"
            >
              <template v-slot:activator="{ on }">
                <v-text-field
                  v-model="scholarship.deadline"
                  label="Due Date"
                  prepend-icon="event"
                  readonly
                  :rules="[minLength_rules.min]"
                  v-on="on"
                ></v-text-field>
              </template>
              <v-date-picker v-model="dueDate" no-title scrollable>
                <v-spacer></v-spacer>
                <v-btn flat color="primary" @click="menu1 = false">Cancel</v-btn>
                <v-btn flat color="primary" @click="$refs.menu1.save(dueDate)">OK</v-btn>
              </v-date-picker>
            </v-menu>
          </v-flex>

          <v-flex xs12 md1>
                <v-text-field
                    v-model="scholarship.requiredGpa"
                    label="Min GPA"
                    required
                    :rules="[gpa_rules.range, minLength_rules.min]"
                ></v-text-field>
                </v-flex>

                <v-flex xs12>
                    <v-textarea
                    outline
                    height="400"
                    v-model = "scholarship.description"
                    label="Scholarship Description"
                    :rules="[minLength_rules.min]"
                    ></v-textarea>
                </v-flex>


            </v-layout>
            </v-container>
        </v-form>

        <v-card-actions>
            <v-spacer></v-spacer>
             <v-btn  flat @click="sheet = false" >cancel</v-btn>
            <v-btn color="primary" @click="updateFields(scholarship)"   :disabled="!addDisabled" flat >SAVE</v-btn>
        </v-card-actions>

        </v-card>

  </div>
    </v-dialog>
    <!-- </div> -->
</template>


<script>
import { store } from "../store.js";
import EDITSCHOLARSHIP from '../graphql/updateScholarship.gql'


  export default {
    data: () => ({
       sheet: false,
      valid: false,
      dueDate: '',
      availableDate: '',
      storeState: store.state,
      addDisabled: true,
      menu1: '',
      menu2: '',
      gpa: '',
      title: '',
      faculty: '',
      amount: '',
      maxLength: 255,
      description: '',
      gpa_rules: {
                range: v => v <= 4.00 && v >= 0.00 && v.length <=4 || 'GPA may only be within 0.00 - 4.00',
            },
      amount_rules: {
                range: v => v <= 999999999.00 && v >= 0.00 || 'Amount must only contain numbers between 0.00 - 999999999.00',
            },
      maxLength_rules: {
         max: v => v.length <= 255|| 'Max character length is 255',
      },
      minLength_rules: {
         min: v => v.length > 0 || 'Required',
      }

    }),
    props: {
        scholarship: Object
    },
    methods: {
       validate () {
        if (this.$refs.editForm.validate()) {
          this.addDisabled = false
        }
      },
        editScholarship(scholarship) {
            if (this.$refs.editForm.validate()) {
                this.$apollo.mutate({
                    mutation: EDITSCHOLARSHIP,
                    variables: {
                      id: scholarship.id,                
                      input: {
                          available: this.availableDate,
                          deadline: this.dueDate,
                          description: this.description,
                          dollarAmount: this.amount,
                          faculty: this.faculty,
                          requiredGpa: this.gpa,
                          title: scholarship.title,
                          visible: true,
                          
                      }
                    }

                }).then( (data) => {
                    this.$emit('showSnackbar', 'Scholarship successfully updated', 'success')
                    this.sheet = false

                }).catch( (error) => {
                    this.$emit('showSnackbar', 'Scholarship update failed', 'error')
                    //this.text = error
                    //this.color = "error"
                    //this.snackbar = true
              
                })
                
            }
          },
          updateFields (scholarship) {
            this.dueDate =  scholarship.deadline
            this.availableDate = scholarship.available
            this.gpa = scholarship.requiredGpa
            this.title =  scholarship.title
            this.faculty = scholarship.faculty
            this.amount = scholarship.dollarAmount
            this.description = scholarship.description
            this.editScholarship(scholarship)
          }

    } 
  }
</script> 
 
<style> 
  .scroll { 
    overflow-y: auto; 
  } 
</style>


2 个答案:

答案 0 :(得分:0)

这是预期的行为。如果对话框在关闭时未从DOM中删除,则除非保留其他状态,否则它将保留其先前状态。

事后看来,您可以选择三种我可以想到的方法中的一种,您正在寻找其中的两种。

  1. 不使用时销毁模态,打开时重新实例化。一个简单的v-if切换一个布尔值就可以解决问题,如果对话框是一个单独的vue实例,则可以使用this.$destroy
  2. 在提交或取消事件上添加this.$el.scrollTop = 0。 (A *)
  3. 在打开的对话框方法中添加scrollWrapper.scrollTop = 0。 (B *)

A :项目2上的this.$el仅在滚动$el元素时才有效,否则,您可以使用{{1}访问目标元素}

B :与项目编号2相同,您应参考实际的滚动包装。

答案 1 :(得分:0)

请将此代码添加到事件中以在对话框中启用。

this.$refs.editForm.$el.scrollIntoView({behavior: 'smooth'})