javascript - 单击菜单选项后,整个页面会随之滚动吗?我该如何阻止呢?

标签 javascript jquery html css

一旦我点击一个菜单选项,整个页面就会随之滚动?我该如何阻止它?

位于jsfiddle中的第411行

<style>@import url(http://fonts.googleapis.com/css?family=Oswald:400,300|Raleway:400,600,300|Source+Sans+Pro:400,600,300);
    @charset 'UTF-8';
    /* Base Styles */
    #cssmenu,
    #cssmenu ul,
    #cssmenu li,
    #cssmenu a {
      margin: 0;
      padding: 0;
      border: 0;
      list-style: none;
      font-weight: normal;
      text-decoration: none;
      line-height: 1;
      font-family: 'Oswald', sans-serif;
      font-weight: 400;
      font-size: 15px;
      position: relative;



    }
    #cssmenu a {
      line-height: 1.3;
    }
    #cssmenu {
      width: 200px;

    }
    #cssmenu > ul > li > a {
      padding-right: 40px;
      font-size: 13px;
      font-weight: bold;
      display: block;
      background: #000000;
      color: #9C9C9C;
      border-bottom: 1px solid #393939;
      text-transform: uppercase;

    }
    #cssmenu > ul > li > a > span {
      background: #000000;
      padding: 10px;
      display: block;
      font-size: 15px;
      font-weight: 300;
    }
    #cssmenu > ul > li > a:hover {
      text-decoration: none;
    }
    #cssmenu > ul > li.active {
      border-bottom: none;
    }
    #cssmenu > ul > li.active > a {
      color: #fff;
    }
    #cssmenu > ul > li.active > a span {
      background: #000000;
    }
    #cssmenu span.cnt {
      position: absolute;
      top: px;
      right: 15px;
      padding: 0;
      margin: 0;
      background: none;
      padding-top: 20px;
    }
    /* Sub menu */
    #cssmenu ul ul {
      display: none;
    }
    #cssmenu ul ul li {
      border: 1px solid #2A2A2A;
      border-top: 0;
    }
    #cssmenu ul ul a {
      padding: 10px;
      display: block;
      color: #FFFFFF;
      font-size: 13px;
      font-weight: 200;
      font-family: 'Source Sans Pro', sans-serif;
    }
    #cssmenu ul ul a:hover {
      color: gray;
    }
    #cssmenu ul ul li.odd {
      background: #2E2E2E;
    }
    #cssmenu ul ul li.even {
      background: #2E2E2E;
    }</style>

    <style>
        .class_slidshow2 {
       padding-left: 200px;
    }
    .rslides li {
      -webkit-backface-visibility: hidden;
      position: absolute;
      display: none;
      width: 100%;
      left: 0;
      top: 0;
      padding-left: 200px;
      }



    <style>.slideshow {
        font-size: 30px;
        text-transform: uppercase;
        font-family: Arial, Verdana, sans-serif;
        color: #a81c11;
        display:inline;
        width: 80%; /* or dependent on what you like */
        float: left;

    } 

    .slideshow {
        float: right;
        display:inline;
        width: 20%; /* relative to width for introtekst */

    }</style>

我还有几个有其他问题。滚动时如何将整个导航菜单保持或“粘贴”到右侧?在更详细的问题中。我希望我的内容位于导航菜单的右侧,会有很多内容,因此一旦用户滚动,他们应该仍然能够看到导航菜单而无需向上滚动,这是我个人的诉求。

位于jsfiddle中的第411行

    <style>@import url(http://fonts.googleapis.com/css?family=Oswald:400,300|Raleway:400,600,300|Source+Sans+Pro:400,600,300);
@charset 'UTF-8';
/* Base Styles */
#cssmenu,
#cssmenu ul,
#cssmenu li,
#cssmenu a {
  margin: 0;
  padding: 0;
  border: 0;
  list-style: none;
  font-weight: normal;
  text-decoration: none;
  line-height: 1;
  font-family: 'Oswald', sans-serif;
  font-weight: 400;
  font-size: 15px;
  position: relative;



}
#cssmenu a {
  line-height: 1.3;
}
#cssmenu {
  width: 200px;

}
#cssmenu > ul > li > a {
  padding-right: 40px;
  font-size: 13px;
  font-weight: bold;
  display: block;
  background: #000000;
  color: #9C9C9C;
  border-bottom: 1px solid #393939;
  text-transform: uppercase;

}
#cssmenu > ul > li > a > span {
  background: #000000;
  padding: 10px;
  display: block;
  font-size: 15px;
  font-weight: 300;
}
#cssmenu > ul > li > a:hover {
  text-decoration: none;
}
#cssmenu > ul > li.active {
  border-bottom: none;
}
#cssmenu > ul > li.active > a {
  color: #fff;
}
#cssmenu > ul > li.active > a span {
  background: #000000;
}
#cssmenu span.cnt {
  position: absolute;
  top: px;
  right: 15px;
  padding: 0;
  margin: 0;
  background: none;
  padding-top: 20px;
}
/* Sub menu */
#cssmenu ul ul {
  display: none;
}
#cssmenu ul ul li {
  border: 1px solid #2A2A2A;
  border-top: 0;
}
#cssmenu ul ul a {
  padding: 10px;
  display: block;
  color: #FFFFFF;
  font-size: 13px;
  font-weight: 200;
  font-family: 'Source Sans Pro', sans-serif;
}
#cssmenu ul ul a:hover {
  color: gray;
}
#cssmenu ul ul li.odd {
  background: #2E2E2E;
}
#cssmenu ul ul li.even {
  background: #2E2E2E;
}</style>

<style>
    .class_slidshow2 {
   padding-left: 200px;
}
.rslides li {
  -webkit-backface-visibility: hidden;
  position: absolute;
  display: none;
  width: 100%;
  left: 0;
  top: 0;
  padding-left: 200px;
  }



<style>.slideshow {
    font-size: 30px;
    text-transform: uppercase;
    font-family: Arial, Verdana, sans-serif;
    color: #a81c11;
    display:inline;
    width: 80%; /* or dependent on what you like */
    float: left;

} 

.slideshow {
    float: right;
    display:inline;
    width: 20%; /* relative to width for introtekst */

}</style>

我还注意到,在所有浏览器上,当用户滚动时背景会移动。这会导致整个页面看起来迷失方向。我确实尝试过,定位:修复; ,但这没有效果。

Jsfiddle:http://jsfiddle.net/8GzLv/

将整个标记粘贴到:http://www.jmarshall.com/easy/html/testbed.html ,所以你可以看到我在说什么。或者在浏览器中缩小 Jsfiddle。

这是背景的样式:位于 Jsfiddle 的第 334 行

<style>#headerWrapper {
    position: fixed;
    width: 200px;
    height: 100%;
    background-color: black; /* Omit to see body content scroll behind radius.  */
}
#headerBorder{
    border: black; 
    border-radius: 35px; 
    -moz-border-radius: 35px; 
    -webkit-border-radius: 35px; 
    overflow:hidden;
}
#headerContent {
    background-image:url("http://www.fimfiction-static.net/images/story_images/92970_r.png?1364253348");
    background-position: left;
    background-repeat: no-repeat;
    background-size: cover;
    height: 200px;
    opacity: 1;
}</style>

以及如何将内容放到屏幕顶部,并紧挨着导航菜单

最佳答案

如果您希望内容显示在屏幕顶部,并且幻灯片宽度小于 1100 像素,则需要将菜单向左浮动。接下来,如果您希望导航栏不再影响您的内容,则内容必须位于其自己的 div 中。我会将该 div float 到右侧,而不是单独的幻灯片 div。最后,要让导航栏随你滚动,你需要给它一个固定的位置,或者使用一些javascript。无论如何,您似乎想要使用列布局,所以我建议您查一下。

这是 jsfiddle:http://jsfiddle.net/8GzLv/1/

我尽量不对您的代码进行过多更改,因此 css 位于您的样式标签之一中。但是就像我上面提到的,现在幻灯片放映周围有一个 div,带有内容类,菜单周围有一个 div,带有菜单占位符类。

但要查看的重要 css 部分是:

.content {
    width:800px;
    overflow:hidden;
}
.menu-placeholder {
    float:left;
    clear:left;
    min-height:250px;
    width:200px;

}
#cssmenu {
    position:fixed;
    top:150px;
}

关于javascript - 单击菜单选项后,整个页面会随之滚动吗?我该如何阻止呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21784951/

相关文章:

javascript - 如何使用JS/jQuery在一个页面的两个局部 View 之间切换?

javascript - 使用 onsubmit 通过 JavaScript 验证表单

javascript - AJAX jQuery .click 元素在加载时运行函数而不是单击

javascript - 使用 JavaScript 发布到 Restful Java 服务不能像 JSON 一样工作?

html - 通过 HTML 包含自己的字体

javascript - 从 Firebase RTDB 和存储获取数据

javascript - 使用 Knockout JS 映射选项将 Observable 属性添加到映射的 Observable 数组

php - onchange select 做不同的mysql查询

javascript - 未获取单击的 href 标签的索引值

jQuery 更改悬停类的属性