📜  使用 jQuery 打开模态时如何防止 Body 滚动?

📅  最后修改于: 2021-11-24 04:37:01             🧑  作者: Mango

给定一个带有模态的 HTML 文档,任务是在模态处于打开状态时防止 body 元素滚动。使用 JavaScript 可以轻松完成此任务。

方法:这个问题的一个简单解决方案是在打开模态时将 body 元素的“溢出”属性的值设置为“隐藏”,这将禁用所选元素上的滚动。一旦模态关闭,我们将 body 元素的“overflow ”属性设置为“auto ”,以便在 body 元素上启用滚动功能。要确定模态是否打开,我们将使用 JavaScript的 classList.contains()方法检查它的类列表中是否有“隐藏”的 CSS 类。这个“ hidden ”类负责在单击按钮时打开和关闭模态(更改显示属性)。查看给定的示例以更好地理解。

例子:

HTML


  

    
    
  
    

  

  
    
    
  
    
    
  
    
    
        

            Given a graph and a source vertex in              the graph, find shortest paths from              source to all vertices in the given              graph. Dijkstra’s algorithm is very              similar to Prim’s algorithm for minimum             spanning tree. Like Prim’s MST, we              generate a SPT (shortest path tree)              with given source as root.             We maintain two sets, one set contains              vertices included in shortest path tree,             other set includes vertices not yet              included in shortest path tree. At every             step of the algorithm, we find a vertex              which is in the other set (set of not yet             included) and has a minimum distance from             the source. Below are the detailed steps             used in Dijkstra’s algorithm to find the             shortest path from a single source vertex             to all other vertices in the given graph.             Algorithm Create a set sptSet (shortest              path tree set) that keeps track of vertices             included in shortest path tree, i.e., whose             minimum distance from source is calculated              and finalized. Initially, this set is empty.             Assign a distance value to all vertices in              the input graph. Initialize all distance              values as INFINITE.             Assign distance value as 0 for the source              vertex so that it is picked first. While              sptSet doesn’t include all vertices Pick a              vertex u which is not there in sptSet and              has minimum distance value. Include u to              sptSet. Update distance value of all adjacent              vertices of u. To update the distance values,             iterate through all adjacent vertices. For              every adjacent vertex v, if sum of distance              value of u (from source) and weight of edge              u-v, is less than the distance value of v,             then update the distance value of v.                Given a graph and a source vertex in the              graph, find shortest paths from source to             all vertices in the given graph. Dijkstra’s              algorithm is very similar to Prim’s              algorithm for minimum spanning tree.             Like Prim’s MST, we generate a SPT (shortest              path tree) with given source as root.             We maintain two sets, one set contains              vertices included in shortest path tree,              other set includes vertices not yet included             in shortest path tree.             At every step of the algorithm, we find a              vertex which is in the other set (set of not              yet included) and has a minimum distance              from the source.                Below are the detailed steps used in              Dijkstra’s algorithm to find the shortest              path from a single source vertex to all other              vertices in the given graph. Algorithm             Create a set sptSet (shortest path tree set)              that keeps track of vertices included in              shortest path tree, i.e., whose minimum              distance from source is calculated and              finalized. Initially, this set is empty. Assign              a distance value to all vertices in the input              graph. Initialize all distance values as INFINITE.             Assign distance value as 0 for the source              vertex so that it is picked first. While sptSet              doesn’t include all vertices Pick a vertex u              which is not there in sptSet and has minimum              distance value. Include u to sptSet.             Update distance value of all adjacent vertices              of u. To update the distance values, iterate              through all adjacent vertices. For every              adjacent vertex v, if sum of distance value              of u (from source) and weight of edge u-v,              is less than the distance value of v, then              update the distance value of v.         

    
          


输出: