9

I am trying to prevent scrolling only when the lightbox component is open, but cannot seem to do so. I hope to not use any outside libraries or plug-ins to do this.

My App.vue contains the "LightBox" component, so I am assuming the prevent scrolling function should live in the App.vue as well. App.vue snippet:

<template>
  <div class="SocialAlbumWidget">
    <div v-if="isModalVisible && media[activeIndex]">
      <LightBox
        ...
      />

I currently have a "showModal ()" function in the "methods" section, so was thinking of passing that through another function.

Methods:

mothods: {
...
showModal () {
  this.isModalVisible = true
},
closeModal () {
  this.isModalVisible = false
}

I expect the body to have scroll when the"Lightbox" component is closed and disabled when the "Lightbox" component is open. Thanks! Let me know what other code would be useful.

3
  • Post your methods object please Commented Jun 24, 2019 at 14:53
  • codepen.io/whatupnewyork/pen/gNRLgd?editors=1010 Commented Jun 24, 2019 at 15:07
  • @Charlie I just updated it as you can see. In the meanwhile, I will look through the pen you just commented. Commented Jun 24, 2019 at 15:11

2 Answers 2

20

You could use a watcher to react to changes in isModalVisible and disable the scrolling function by using style="overflow: hidden".

Something along these lines:

// HTML
<btn @click="dialog = !dialog" >Click Me </btn>

// JS
new Vue({
  el: '#app',
  data () {
    return {
      dialog: false,
    }
  },
  watch: {
    isModalVisible: function() {
      if(this.isModalVisible){
        document.documentElement.style.overflow = 'hidden'
        return
      }

      document.documentElement.style.overflow = 'auto'
    }
  }
})
Sign up to request clarification or add additional context in comments.

4 Comments

Syntax Error: SyntaxError: /Users/angello.lazar/Desktop/sdc-social-album-widget/src/App.vue: Unexpected token, expected "," (128:35) 126 | }, 127 | watch: { > 128 | isModalVisible: preventScroll(){
That makes sense - thanks. But, not sure why I am receiving this error...
Figured it out! Thanks for the help!
thats not work on ios devices. U need fixed content and wach page scroll posotion, then add negative position top to your fixed wrap. after, when scroll turn on, u need scrollTop() on js to prev position.
18

Prevent scrolling events on LightBox modal itself -

<LightBox
 @wheel.prevent
 @touchmove.prevent
 @scroll.prevent
/>

style overflow: hidden might create some concerns.

such as;

  • Visibility of scrollbar
  • UI bounce w.e.f overflow toggle

3 Comments

@touchmove.prevent worked for me in mobile. I liked it
This doesn't seem to prevent dragging the scroll bars.
@killjoy does the Modal has background overlay?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.