WPF轮播图动画交互 动画缩放展示图片
C#代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace Caroursel
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- SizeChanged += MainWindow_SizeChanged;
- }
- private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- var targetWidth = Width / 5;
- DoubleAnimation da = new DoubleAnimation(targetWidth, new Duration(TimeSpan.FromSeconds(0.25)));
- Grid1.BeginAnimation(WidthProperty, da);
- Grid2.BeginAnimation(WidthProperty, da);
- Grid3.BeginAnimation(WidthProperty, da);
- Grid4.BeginAnimation(WidthProperty, da);
- Grid5.BeginAnimation(WidthProperty, da);
- }
- private void Grid_MouseEnter(object sender, MouseEventArgs e)
- {
- var grid = sender as Grid;
- var targetWidth = Width / 5 * 3;
- DoubleAnimation da = new DoubleAnimation(targetWidth, new Duration(TimeSpan.FromSeconds(0.3)));
- da.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
- grid.BeginAnimation(WidthProperty, da);
- var remainingWidth = Width - targetWidth;
- targetWidth = remainingWidth / 4;
- da = new DoubleAnimation(targetWidth, new Duration(TimeSpan.FromSeconds(0.25)));
- if (grid != Grid1)
- {
- Grid1.BeginAnimation(WidthProperty, da);
- }
- if (grid != Grid2)
- {
- Grid2.BeginAnimation(WidthProperty, da);
- }
- if (grid != Grid3)
- {
- Grid3.BeginAnimation(WidthProperty, da);
- }
- if (grid != Grid4)
- {
- Grid4.BeginAnimation(WidthProperty, da);
- }
- if (grid != Grid5)
- {
- Grid5.BeginAnimation(WidthProperty, da);
- }
- }
- private void Grid_MouseLeave(object sender, MouseEventArgs e)
- {
- var targetWidth = Width / 5;
- DoubleAnimation da = new DoubleAnimation(targetWidth, new Duration(TimeSpan.FromSeconds(0.25)));
- Grid1.BeginAnimation(WidthProperty, da);
- Grid2.BeginAnimation(WidthProperty, da);
- Grid3.BeginAnimation(WidthProperty, da);
- Grid4.BeginAnimation(WidthProperty, da);
- Grid5.BeginAnimation(WidthProperty, da);
- }
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |