【问题标题】:Shrink a div without jquery缩小没有jquery的div
【发布时间】:2019-05-13 05:15:46
【问题描述】:

在我的角度应用程序中,我的标题与格式一样,

-- Header --

-- Sub header --

-- Search Box --

-- Create and Search Button --

-- Scroll Div --

HTML:

<h1> Header </h1>
<h3> Sub header </h3>
<div class="search-box-wrapper">
    <input class="search-box" type="text" placeholder="search"> <br><br><br>
    <button type="button"> Create </button> &nbsp; &nbsp;
    <button type="button"> Search </button> <br><br>
</div>

<div class="scroll-div">
  <ul>
    <li> <h1> User One </h1> </li>
    <li> <h1> User Two </h1> </li>
    <li> <h1> User Three </h1> </li>
    <li> <h1> User Four </h1> </li>
    <li> <h1> User Five </h1> </li>
    <li> <h1> User Six </h1> </li>
    <li> <h1> User Seven </h1> </li>
    <li> <h1> User Eight </h1> </li>
    <li> <h1> User Nine </h1> </li>
    <li> <h1> User Ten </h1> </li>
     </ul>
  </div> 

CSS:

.search-box {
   border-radius: 25px;
    border: 5px solid #000;
    padding: 10px;
    width: 90%;
}

.scroll-div {
   height: calc(100vh - 400px);
   overflow: scroll;
}

工作堆栈闪电战 https://stackblitz.com/edit/angular-vhnt8q

这里我有一个类名为scroll-div 的div,其中列表项可滚动..

如果我开始滚动,那么我需要缩小search-wrapper(创建和搜索按钮被隐藏)..

再次滚动到最高点应该是正常的..

预期的输出类似于谷歌搜索..

初始搜索结果会像,

而在 scroll start 上它会像这样收缩,

以同样的方式,我需要在滚动开始时隐藏创建和搜索按钮并仅显示搜索框(缩小),并且在滚动顶部再次需要显示创建和搜索按钮..

请帮助我实现预期的结果无需 jquery ..

任何角度方式的结果都会对我有更多帮助..

【问题讨论】:

  • 你的意思是粘性导航栏?
  • 兄弟,您是否在没有 jquery 或第三方库的情况下使用纯 angular 和 typescript 在任何滑块中进行了锻炼?如果您有或找到任何请分享链接兄弟..
  • @Chellappan,谢谢兄弟..

标签: javascript html css angular typescript


【解决方案1】:

要隐藏和显示元素,您可以通过 元素 id

获得

在 .html 和 .ts 文件上使用事件方法添加以下函数:

html: (scroll)="scroll($event.target.value) 用于检测滚动

ts:

scroll(val) {
    let scroll = document.getElementById('scroll');

    if (scroll.scrollTop == 0) {
      this.isShow = 'show';
    } else {
      this.isShow = 'hide';
    }
  }

检查以下stackblitz fork


添加需要导入的动画:

import {
    trigger,
    state,
    style,
    animate,
    transition
} from '@angular/animations';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

然后添加

 animations: [
     trigger('toggleHeight', [
            state('hide', style({
                height: '0px',
                opacity: '0',
                overflow: 'hidden',
                // display: 'none'
            })),
            state('show', style({
                height: '*',
                opacity: '1',
                // display: 'block'
            })),
            transition('hide => show', animate('200ms ease-in')),
            transition('show => hide', animate('200ms ease-out'))
        ])
    ],

不要忘记包括以下内容: import { BrowserAnimationsModule } from '@angular/platform-browser/animations';BrowserAnimationsModule 关于模块 @NgModule 导入

有关角度信息的更多信息,您可以查看here / demo

【讨论】:

  • 您的解决方案更接近我的需要。如果您添加收缩效果(类似动画过渡)而不是突然显示/隐藏 div 会更好。
  • 如何仅在滚动开始时输入搜索框后删除中断??..因为我在滚动开始时在那个地方得到了很少的空白。如果可能,请更新 stackblitz 并删除它滚动开始时的三个&lt;br&gt;&lt;br&gt;&lt;br&gt; 标签..
  • 只需将
    语法放在 div 中。检查更新。或者还不如删除
    语法并修改
    与 style="margin-top: value;"其中 value 是您想要的边距
【解决方案2】:

使用布尔参数和滚动设置flase 并使用*ngIf 显示/隐藏按钮并检查是否event.target.scrollTop &gt; 0

See blitz

HTML

<h1> Header </h1>
<h3> Sub header </h3>
<div class="search-box-wrapper">
    <input class="search-box" type="text" placeholder="search"> <br><br><br>
    <button type="button" *ngIf="scrollable"> Create </button> &nbsp; &nbsp;
    <button type="button" *ngIf="scrollable"> Search </button> <br><br>
</div>
<p> <b> While Scroll startes in the below div and shrink the above create and search </b> </p>
<div class="scroll-div" (scroll)="onscroll($event)">
  <ul>
    <li> <h1> User One </h1> </li>
    <li> <h1> User Two </h1> </li>
    <li> <h1> User Three </h1> </li>
    <li> <h1> User Four </h1> </li>
    <li> <h1> User Five </h1> </li>
    <li> <h1> User Six </h1> </li>
    <li> <h1> User Seven </h1> </li>
    <li> <h1> User Eight </h1> </li>
    <li> <h1> User Nine </h1> </li>
    <li> <h1> User Ten </h1> </li>
     </ul>
  </div>

TS

  scrollable:boolean=true;
  onscroll(event:any){
    console.log(event.target.scrollTop)
    if (event.target.scrollTop > 0) {
      this.scrollable=false;
    }
    else{
      this.scrollable=true;
    }
  }

【讨论】:

  • 不,它隐藏在您解决方案中的那个地方我需要缩小它。意味着下面的内容(scroll-div)需要滚动到顶部(搜索框下方)(作为创建和搜索按钮被隐藏)..如果滚动再次到达顶部,您的解决方案中会发生什么?我什么都看不到..
  • 我需要将滚动 div 置于搜索框下方。在您的解决方案中,仅创建和搜索按钮一直隐藏。与另一个解决方案一样,它应该移至搜索下方的顶部盒子,但我需要收缩效果..
  • 你有什么例子或css代码吗?
  • 是的,我在问题中提到的期望就像谷歌搜索..输入任何单词后搜索..开始滚动后,所有菜单都将隐藏,除了搜索框..效果也将是平滑收缩..然后滚动到顶部并使用平滑收缩效果再次显示所有菜单..
  • 在您的解决方案中,创建和搜索按钮从该位置隐藏并显示在同一位置。但scroll-div 需要放在顶部,因为在创建和搜索按钮的位置创建了一个空白区域..
【解决方案3】:

您可以仅通过 CSS3 实现粘性导航栏。希望对你有用。

HTML:app.component.html

<h1> Header </h1>
<h3> Sub header </h3>
<div class="search-container">
  <div class="search-box-wrapper">
      <input class="search-box" type="text" placeholder="search">
      <div class="search-box-btn">
        <button type="button"> Create </button>
        <button type="button"> Search </button>
      </div>
  </div>
  <div class="scroll-div">
    <ul>
      <li> <h1> User One </h1> </li>
      <li> <h1> User Two </h1> </li>
      <li> <h1> User Three </h1> </li>
      <li> <h1> User Four </h1> </li>
      <li> <h1> User Five </h1> </li>
      <li> <h1> User Six </h1> </li>
      <li> <h1> User Seven </h1> </li>
      <li> <h1> User Eight </h1> </li>
      <li> <h1> User Nine </h1> </li>
      <li> <h1> User Ten </h1> </li>
      </ul>
  </div>
</div>

CSS : app.component.css

.search-container{
    position: relative;
    overflow-y: auto;
    overflow-x: hidden;
    max-height: calc(100vh - 400px);
}
.search-box-wrapper{
  position: sticky;
  width:100%;
  top:0px;
  background-color:#fff;
  padding-bottom:10px;
}
.search-box-wrapper .search-box {
    border-radius: 25px;
    border: 5px solid #000;
    padding: 10px;
}
.search-box-wrapper input{
    width: 100%;
}
.search-box-wrapper .search-box-btn{
    margin-top:10px;
    width: 100%;
    margin-left: 25px;
}
.search-box-wrapper button{
  margin-right:20px;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多