我有一些代码,当用户向下滚动并点击该点时,它将启动动画。我希望它向下滑动,但目前只有滑动切换工作 - 它不断打开和关闭。我怎样才能让它滑下来?
$(window).scroll( function(){
/* Check the location of each desired element */
$('.research-revealed').each( function(i){
var bottom_of_object = $(this).offset().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* If the object is completely visible in the window, fade it it */
if( bottom_of_window > bottom_of_object ){
$(this).slideToggle( "slow", function() {
// Animation complete.
});
}
});
});
答案 0 :(得分:1)
一旦达到所需的点,设置一个标志以指示它已设置动画。
var animated = false;
$(window).scroll( function(){
/* Check the location of each desired element */
$('.research-revealed').each( function(i){
var bottom_of_object = $(this).offset().top + $(this).outerHeight();
var bottom_of_window = $(window).scrollTop() + $(window).height();
/* If the object is completely visible in the window, fade it it */
if( bottom_of_window > bottom_of_object && animated == false ){
animated = true;
$(this).slideToggle( "slow", function() {
// Animation complete.
});
}
});
});
您也不应将此绑定到窗口滚动事件,而是使用requestAnimationFrame或以间隔限制事件。您也应该在触发动画后删除该事件。
[更新] 更进一步......
所以这里是我用来处理这类事情的模块组合。我写这个是为了特定的目的,指出元素何时在视图内或视图外,以及跟踪用户滚动的方向。
http://codepen.io/jasonhulbert/pen/ZbNoZG
在JS中,您将看到一个polyfill和两个模块。我通常不会将它们包含在此处看到的相同文件中,但我只是粘贴它们并将它们包装在SEAF闭包中。我通常将它们用作commonJS模块。 polyfill用于window.requestAnimationFrame,我创建的两个模块是FrameEvent和InView。
FrameEvent是一个简单的脚本,用于管理滚动和调整使用requestAnimationFrame的回调。
InView是我用来跟踪视图“在”或“视线外”(窗口)的元素。当用户滚动时,它会将属性data-inview
的值切换为in
或out
。它还指示属性scroll-direction
中正文的滚动方向。您还可以关联元素进入或退出视图时触发的回调(这在您的情况下很有用)。
InView实例化如此(这包含在JS的底部):
var inview = new InView();
inview.addStage({
element: '.item'
});
以下是您可以传递给.addStage()
的所有选项:
element: 'section', // the element to look for
attr: 'data-inview', // the name of the inview attribute
inVal: 'in', // the value of the attribute when in view
outVal: 'out', // the value of the attribute when out of view
offsetTop: 0, // pixels
offsetBottom: 0, // pixels
delayIn: 0, // in milliseconds
delayOut: 0, // in milliseconds
inCallback: false, // callback when element comes into view
outCallback: false // callback when element goes out of view
因此,在您的情况下,您可以使用InView(A)通过使用css中的选择器(如.item[data-inview="in"] { //...props }
)或(B)将inCallback
选项设置为函数来应用css属性触发您当前使用的动画。回调中this
的上下文将是元素。
所以,就是这样。
var inview = InView();
inview.addStage({
element: '.item', // the element you're looking for
offsetTop: 100, // wait until the element is 100px within view
inCallback: function(el) {
$(el).slideToggle(); // do your animation stuffs
}
});
即使您不想使用它,也可以查看.checkStagePositions
方法,以查看如何计算元素相对于窗口的位置的示例。我知道,这个函数有点混乱 - 我需要进行一些重构,然后将一些逻辑划分为不同的方法。
答案 1 :(得分:0)
<Style x:Key="{x:Type ribbon:RibbonMenuButton}" TargetType="{x:Type ribbon:RibbonMenuButton}">
<Style.Resources>
<Style TargetType="{x:Type ribbon:RibbonSeparator}">
<Setter Property="FrameworkElement.OverridesDefaultStyle" Value="True" />
<Setter Property="UIElement.Focusable" Value="False" />
<Setter Property="Control.BorderBrush" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ribbon:RibbonMenuButton}}, Path=Ribbon.BorderBrush}" />
<Setter Property="Control.Background" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ribbon:RibbonMenuButton}}, Path=Ribbon.Background}" />
<Setter Property="Control.FontWeight" Value="Bold" />
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ribbon:RibbonSeparator}">
<Border Name="MainBorder" SnapsToDevicePixels="True" BorderThickness="0,1,0,1" BorderBrush="{TemplateBinding Control.BorderBrush}" Background="{TemplateBinding Control.Background}">
<Border Name="Overlay" Background="{StaticResource ä}">
<TextBlock Name="Text" Margin="2,1,2,1" Text="{TemplateBinding ribbon:RibbonSeparator.Label}" />
</Border>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Value="True" Binding="{Binding Path=HighContrast, Source={x:Static shell:SystemParameters2.Current}}">
<Setter TargetName="Text" Value="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" Property="TextBlock.Foreground" />
<Setter TargetName="Overlay" Property="Border.Background" Value="#00FFFFFF" />
<Setter TargetName="MainBorder" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" Property="Border.BorderBrush" />
<Setter TargetName="MainBorder" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Property="Border.Background" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="ribbon:RibbonSeparator.Label" Value="{x:Null}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ribbon:RibbonSeparator}">
<Grid Name="Grid" SnapsToDevicePixels="True" Margin="1">
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="22" Width="Auto" SharedSizeGroup="MenuItemIconColumnGroup" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="14" />
</Grid.ColumnDefinitions>
<Border Name="SideBarBorder" BorderThickness="0,0,1,0" Margin="0,-1,0,-1" Background="{TemplateBinding Control.Background}" BorderBrush="{TemplateBinding Control.BorderBrush}">
<Border Name="SideBarOverlay" Background="{StaticResource ä}" />
</Border>
<Line Name="Line" Grid.Column="1" Grid.ColumnSpan="2" Margin="2,0,0,0" X1="0" Y1="0" X2="1" Y2="0" Stroke="{TemplateBinding Control.BorderBrush}" StrokeThickness="1" Stretch="Fill" />
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Value="True" Binding="{Binding Path=HighContrast, Source={x:Static shell:SystemParameters2.Current}}">
<Setter TargetName="SideBarBorder" Property="Border.Background" Value="#00FFFFFF" />
<Setter TargetName="SideBarBorder" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" Property="Border.BorderBrush" />
<Setter TargetName="SideBarOverlay" Property="Border.Background" Value="#00FFFFFF" />
<Setter TargetName="Line" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" Property="Shape.Stroke" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
<Setter Property="Control.Background" Value="#00FFFFFF" />
<Setter Property="Control.BorderBrush" Value="#00FFFFFF" />
<Setter Property="Control.BorderThickness" Value="1" />
<Setter Property="FrameworkElement.FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Control.HorizontalContentAlignment" Value="Center" />
<Setter Property="Control.Padding" Value="2,0,2,0" />
<Setter Property="ribbon:RibbonMenuButton.MouseOverBorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=Ribbon.MouseOverBorderBrush}" />
<Setter Property="ribbon:RibbonMenuButton.MouseOverBackground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Ribbon.MouseOverBackground}" />
<Setter Property="ribbon:RibbonMenuButton.PressedBorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=Ribbon.PressedBorderBrush}" />
<Setter Property="ribbon:RibbonMenuButton.PressedBackground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Ribbon.PressedBackground}" />
<Setter Property="ribbon:RibbonMenuButton.FocusedBorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=Ribbon.FocusedBorderBrush}" />
<Setter Property="ribbon:RibbonMenuButton.FocusedBackground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Ribbon.FocusedBackground}" />
<Setter Property="ToolTipService.InitialShowDelay" Value="{StaticResource à}" />
<Setter Property="ToolTipService.ShowDuration" Value="{StaticResource á}" />
<Setter Property="ToolTipService.BetweenShowDelay" Value="{StaticResource â}" />
<Setter Property="ribbon:RibbonTwoLineText.PathData" Value="{StaticResource ÿ}" />
<Setter Property="ribbon:RibbonMenuButton.QuickAccessToolBarControlSizeDefinition">
<Setter.Value>
<rcp:RibbonControlSizeDefinition ImageSize="Small" IsLabelVisible="False" />
</Setter.Value>
</Setter>
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ribbon:RibbonMenuButton}">
<Grid Name="MainGrid" SnapsToDevicePixels="True">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<rcp:RibbonToggleButton x:Name="PART_ToggleButton" Template="{StaticResource Ĥ}" RibbonTwoLineText.PathData="{TemplateBinding ribbon:RibbonTwoLineText.PathData}" Label="{TemplateBinding ribbon:RibbonMenuButton.Label}" LargeImageSource="{TemplateBinding ribbon:RibbonMenuButton.LargeImageSource}" SmallImageSource="{TemplateBinding ribbon:RibbonMenuButton.SmallImageSource}" ControlSizeDefinition="{TemplateBinding ribbon:RibbonMenuButton.ControlSizeDefinition}" BorderBrush="{TemplateBinding Control.BorderBrush}" BorderThickness="{TemplateBinding Control.BorderThickness}" Background="{TemplateBinding Control.Background}" CornerRadius="2" MouseOverBorderBrush="{TemplateBinding ribbon:RibbonMenuButton.MouseOverBorderBrush}" MouseOverBackground="{TemplateBinding ribbon:RibbonMenuButton.MouseOverBackground}" CheckedBorderBrush="{TemplateBinding ribbon:RibbonMenuButton.PressedBorderBrush}" CheckedBackground="{TemplateBinding ribbon:RibbonMenuButton.PressedBackground}" FocusedBorderBrush="{TemplateBinding ribbon:RibbonMenuButton.FocusedBorderBrush}" FocusedBackground="{TemplateBinding ribbon:RibbonMenuButton.FocusedBackground}" HorizontalContentAlignment="{TemplateBinding Control.HorizontalContentAlignment}" Padding="{TemplateBinding Control.Padding}" ClickMode="Press" Style="{x:Null}" FocusVisualStyle="{x:Null}" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}">
<rcp:RibbonToggleButton.Resources>
<Thickness Left="1" Top="1" Right="1" Bottom="2">
<ComponentResourceKey.Key TypeInTargetAssembly="{x:Type ribbon:Ribbon}" ResourceId="LargeImageMargin" />
</Thickness>
</rcp:RibbonToggleButton.Resources>
</rcp:RibbonToggleButton>
<Popup Name="PART_Popup" HorizontalOffset="1" VerticalOffset="-1" AllowsTransparency="True" Placement="Bottom" Focusable="False" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen}">
<sdsc:SystemDropShadowChrome Name="Shadow" Color="Transparent" p15:KeyTipService.IsKeyTipScope="True" RenderOptions.ClearTypeHint="Enabled" xmlns:p15="clr-namespace:Microsoft.Windows.Controls;assembly=RibbonControlsLibrary,Version=4.0.0.11019,Culture=neutral,PublicKeyToken=31bf3856ad364e35">
<Border Name="MenuBorder" BorderThickness="{TemplateBinding Control.BorderThickness}" CornerRadius="2" BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Ribbon.BorderBrush}" Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Ribbon.Background}">
<Border Name="SubMenuInnerBorder" Background="{StaticResource ï}" BorderThickness="0" CornerRadius="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ScrollViewer Name="PART_SubMenuScrollViewer" Grid.Row="1" Margin="1" Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type FrameworkElement}, ResourceId=MenuScrollViewer}}">
<Grid RenderOptions.ClearTypeHint="Enabled">
<Canvas Name="BackgroundCanvas" Height="0" Width="0" HorizontalAlignment="Left" VerticalAlignment="Top">
<Rectangle Name="OpaqueRect" RadiusX="2" RadiusY="2" Height="{Binding ElementName=MenuBorder, Path=ActualHeight}" Width="{Binding ElementName=MenuBorder, Path=ActualWidth}" Fill="{Binding ElementName=MenuBorder, Path=Background}" />
<Rectangle Name="OverlayRect" RadiusX="2" RadiusY="2" Height="{Binding ElementName=SubMenuInnerBorder, Path=ActualHeight}" Width="{Binding ElementName=SubMenuInnerBorder, Path=ActualWidth}" Fill="{Binding ElementName=SubMenuInnerBorder, Path=Background}" />
</Canvas>
<ItemsPresenter Name="ItemsPresenter" KeyboardNavigation.TabNavigation="Cycle" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" />
</Grid>
</ScrollViewer>
<Border Name="ResizeControl" Grid.Row="2" Visibility="Collapsed" Background="{StaticResource æ}" BorderThickness="0,1,0,0" BorderBrush="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Ribbon.BorderBrush}">
<Thumb Name="PART_ResizeThumb" Style="{StaticResource ç}" />
</Border>
</Grid>
</Border>
</Border>
</sdsc:SystemDropShadowChrome>
</Popup>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="PART_SubMenuScrollViewer" Property="ScrollViewer.CanContentScroll" Value="False">
<Setter TargetName="OpaqueRect" Value="{Binding ElementName=PART_SubMenuScrollViewer, Path=VerticalOffset}" Property="Canvas.Top" />
<Setter TargetName="OpaqueRect" Value="{Binding ElementName=PART_SubMenuScrollViewer, Path=HorizontalOffset}" Property="Canvas.Left" />
<Setter TargetName="OverlayRect" Value="{Binding ElementName=PART_SubMenuScrollViewer, Path=VerticalOffset}" Property="Canvas.Top" />
<Setter TargetName="OverlayRect" Value="{Binding ElementName=PART_SubMenuScrollViewer, Path=HorizontalOffset}" Property="Canvas.Left" />
</Trigger>
<DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsInQuickAccessToolBar}">
<Setter TargetName="PART_ToggleButton" Property="FrameworkElement.Height" Value="Auto" />
</DataTrigger>
<Trigger Property="UIElement.IsEnabled" Value="False">
<Setter TargetName="MainGrid" Value="{StaticResource Ë}" Property="TextElement.Foreground" />
</Trigger>
<Trigger Property="ribbon:RibbonMenuButton.HasGallery" Value="True">
<Setter TargetName="PART_SubMenuScrollViewer" Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
<Setter TargetName="PART_SubMenuScrollViewer" Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="ribbon:RibbonMenuButton.CanUserResizeHorizontally" Value="True" />
<Condition Property="ribbon:RibbonMenuButton.CanUserResizeVertically" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="ResizeControl" Property="UIElement.Visibility" Value="Visible" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="ribbon:RibbonMenuButton.CanUserResizeHorizontally" Value="False" />
<Condition Property="ribbon:RibbonMenuButton.CanUserResizeVertically" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="ResizeControl" Property="UIElement.Visibility" Value="Visible" />
<Setter TargetName="PART_ResizeThumb" Value="{StaticResource è}" Property="FrameworkElement.Style" />
</MultiTrigger>
<Trigger Property="ribbon:RibbonMenuButton.IsDropDownPositionedAbove" Value="True">
<Setter TargetName="ResizeControl" Property="Grid.Row" Value="0" />
<Setter TargetName="ResizeControl" Property="Border.BorderThickness" Value="0,0,0,1" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="ribbon:RibbonMenuButton.IsDropDownPositionedAbove" Value="True" />
<Condition Property="ribbon:RibbonMenuButton.CanUserResizeHorizontally" Value="True" />
<Condition Property="ribbon:RibbonMenuButton.CanUserResizeVertically" Value="True" />
</MultiTrigger.Conditions>
<Setter TargetName="PART_ResizeThumb" Value="{StaticResource é}" Property="FrameworkElement.Style" />
</MultiTrigger>
<Trigger Property="ItemsControl.HasItems" Value="False">
<Setter TargetName="PART_ToggleButton" Value="{x:Null}" Property="ribbon:RibbonTwoLineText.PathData" />
</Trigger>
<Trigger SourceName="PART_Popup" Property="Popup.HasDropShadow" Value="True">
<Setter TargetName="Shadow" Property="FrameworkElement.Margin" Value="0,0,5,5" />
<Setter TargetName="Shadow" Value="{StaticResource Õ}" Property="classic:SystemDropShadowChrome.Color" />
</Trigger>
<Trigger SourceName="PART_Popup" Property="Popup.IsOpen" Value="False">
<Setter TargetName="PART_Popup" Property="Popup.PopupAnimation" Value="None" />
</Trigger>
<DataTrigger Value="True" Binding="{Binding Path=HighContrast, Source={x:Static shell:SystemParameters2.Current}}">
<Setter TargetName="BackgroundCanvas" Property="UIElement.Visibility" Value="Collapsed" />
<Setter TargetName="Shadow" Property="classic:SystemDropShadowChrome.Color" Value="Transparent" />
<Setter TargetName="PART_Popup" Property="Popup.PopupAnimation" Value="None" />
<Setter TargetName="PART_SubMenuScrollViewer" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" Property="Control.Foreground" />
<Setter TargetName="MenuBorder" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" Property="Border.BorderBrush" />
<Setter TargetName="MenuBorder" Value="{DynamicResource {x:Static SystemColors.MenuBrushKey}}" Property="Border.Background" />
<Setter TargetName="MenuBorder" Property="Border.CornerRadius" Value="0" />
<Setter TargetName="ResizeControl" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" Property="Border.BorderBrush" />
<Setter TargetName="ResizeControl" Value="{DynamicResource {x:Static SystemColors.MenuBrushKey}}" Property="Border.Background" />
<Setter TargetName="SubMenuInnerBorder" Property="Border.Background" Value="#00FFFFFF" />
<Setter TargetName="SubMenuInnerBorder" Property="Border.CornerRadius" Value="0" />
<Setter TargetName="MainGrid" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" Property="TextElement.Foreground" />
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}" Value="False" />
<Condition Binding="{Binding Path=HighContrast, Source={x:Static shell:SystemParameters2.Current}}" Value="True" />
</MultiDataTrigger.Conditions>
<Setter TargetName="MainGrid" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" Property="TextElement.Foreground" />
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="ribbon:RibbonMenuButton.IsDropDownOpen" Value="True">
<Setter Property="ToolTipService.IsEnabled" Value="False" />
</Trigger>
</Style.Triggers>
</Style>
slideDown() - Description: Display the matched elements with a sliding motion.
因此,如果要隐藏元素,则必须使用slideUp() - Description: Hide the matched elements with a sliding motion.
。别担心,看起来它会滑下来。见FIDDLE