How to add a mouse hover (hover) effect to the home page banner?

As an experienced website operations expert, I fully understand your pursuit of website visual effects and user experience.The homepage banner serves as the 'front door' of the website, and its design and interaction directly affect the first impression of visitors.Today, let's delve into how to add an attractive hover effect to the homepage banner in AnQiCMS, making your website more vibrant.

Use CSS cleverly to make the homepage Banner move

In the AnQi CMS, implementing the mouse hover effect for the home page banner is actually much simpler than you imagine.AnQiCMS powerful template system and content management capabilities provide us with great flexibility.:hoverEnglish class, combined with HTML structure and the Banner data calling mechanism of AnQiCMS, you can easily create an eye-catching interactive experience.

Let's first sort out the source and structure of the template files for Banner content in the security CMS.In AnQiCMS backend, you can configure Banner images in the 'Website Navigation Settings' or upload Banner images for specific categories or pages in 'Category Management' and 'Page Management'.These banner data will eventually be called to the front-end page via specific template tags.{% bannerList %}This tag is used to obtain Banner data.

Prepare the HTML skeleton and data call for Banner

In the template system of AnQiCMS, the home page template file is usually located in/templatethe directory of the theme you have selected underindex/index.htmlor it can be directlyindex.htmlWe need to build the HTML structure of the Banner in this file, and use the{% bannerList %}tag to loop out the Banner content.

{% bannerList %}Tags can flexibly retrieve the Banner list you configured in the background. You can specifytypeparameters to call different groups of Banners (for exampletype="首页轮播"),can also directly use the default group. When iterating over Banner data, each Banner item will provideLogo(Image address,)Link(Link address,)Alt(Alternative text for image) andTitle(Title) fields, these are the basic components we use to construct HTML structures.

The following is an example of a basic HTML structure that shows how to use{% bannerList %}Label to render Banner, and add necessary CSS class names to each Banner item for subsequent style control:

<div class="main-banner-container">
    {% bannerList banners with type="default" %} {# 假设调用默认分组的Banner #}
        {% for item in banners %}
            <a href="{{item.Link}}" target="_blank" class="banner-item">
                <img src="{{item.Logo}}" alt="{{item.Alt | default:item.Title}}" class="banner-image">
                <div class="banner-overlay">
                    <h3 class="banner-title">{{item.Title}}</h3>
                    {% if item.Description %}
                        <p class="banner-description">{{item.Description}}</p>
                    {% endif %}
                </div>
            </a>
        {% endfor %}
    {% endbannerList %}
</div>

In this structure:

  • main-banner-containerIs the overall container of the Banner.
  • banner-itemIs each separate Banner item, it is a<a>Label that wraps the image and description.
  • banner-imageIs the Banner image.
  • banner-overlayis a popup, where we can place a title and description, and show or change the style on mouse hover.

inject CSS magic to create hover effects.

Now we have the HTML skeleton, and the next step is to inject CSS styles into it to achieve dynamic effects on mouse hover. These CSS style files are usually stored in/public/static/The theme-related paths under the directory. You can find and edit these CSS files in the "Template Design" feature in the background, or upload them directly via FTP/SFTP.

We can design a common hover effect: when the mouse is moved over the Banner, the image slightly expands, and the text on the overlay becomes fully opaque from semi-transparent, moving slightly upwards to create a sense of information emerging.

/* Banner容器的基本样式 */
.main-banner-container {
    width: 100%;
    overflow: hidden; /* 确保图片放大时不会溢出容器 */
    position: relative;
    /* 如果是轮播,这里可能还需要轮播插件的额外样式 */
}

/* 每个Banner项的样式 */
.banner-item {
    display: block; /* 让a标签充满整个空间 */
    position: relative;
    overflow: hidden;
    cursor: pointer;
    text-decoration: none;
    color: inherit;
    /* 其他布局样式,例如如果是多图并排,这里需要flex或grid布局 */
}

/* Banner图片样式 */
.banner-image {
    width: 100%;
    height: auto;
    display: block;
    transition: transform 0.3s ease-in-out; /* 平滑过渡效果 */
}

/* Banner浮层样式,默认隐藏或半透明 */
.banner-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 20px;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0)); /* 渐变背景 */
    color: #fff;
    opacity: 0; /* 默认不透明度为0,隐藏 */
    transform: translateY(100%); /* 默认向下移动100%,完全隐藏 */
    transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out; /* 平滑过渡 */
    box-sizing: border-box;
}

.banner-overlay .banner-title {
    font-size: 24px;
    margin-bottom: 10px;
}

.banner-overlay .banner-description {
    font-size: 16px;
    line-height: 1.5;
}

/* 鼠标悬停时的效果 */
.banner-item:hover .banner-image {
    transform: scale(1.05); /* 图片放大5% */
}

.banner-item:hover .banner-overlay {
    opacity: 1; /* 浮层完全不透明 */
    transform: translateY(0); /* 浮层向上移动到原位 */
}

Add this CSS code to your theme stylesheet (for examplestyle.cssormain.css), save and refresh the page, and you'll see the dynamic effect of the Banner when the mouse hovers over it.

Improve and optimize

To make the effect more perfect, you can adjust according to your actual needs:

  • Image processing:If the Banner image size is not uniform, consider usingobject-fit: cover;Properties or upload images with consistent dimensions in the background to ensure consistent visual effects.
  • Multi-layer animation:You can setbanner-titleandbanner-descriptionto set different onestransition-delayLet them have a sense of randomness when emerging, adding fun to the animation.
  • Compatibility:Always pay attention to the browser compatibility of CSS properties and add browser prefixes when necessary.

Through the above steps, your security CMS home page banner now has an attractive mouse hover effect.This not only enhances the visual appeal of the website, but also strengthens the user interaction experience through dynamic feedback, allowing visitors to feel the professionalism and vitality of the website more.


Common Questions (FAQ)

Q1: My Banner image sizes are not uniform, how can I ensure that the hover effect is coordinated and not distorted?

A1:The image size discrepancy is indeed a common problem affecting visual coordination.In AnQiCMS, you can start from two aspects.The most direct method is to ensure that all Banner images have the same size ratio (e.g., 16:9 or 4:3) and resolution when uploading them to the backend.object-fitProperties to control the display of images within the container. For example,object-fit: cover;It will fill the container with the image, trimming the excess part to maintain the image without distortion;object-fit: contain;The image will be scaled proportionally to fit the container completely, which may leave some blank space. Combined withoverflow: hidden;It can effectively manage the cropping area when images are zoomed.

Q2: Besides changing the style, can I display more information on hover, such as a button or additional explanation?

A2:Of course, we providebanner-overlayThe structure is designed for this. You canbanner-overlayAdd any HTML element internally, such as a Call-to-Action (CTA) button<a>Additional text paragraph<p>Even small icons and even smaller elements. In CSS, you can control the default visibility of these elements (for exampledisplay: none;oropacity: 0;), and then.banner-item:hover .banner-overlayrules, setting them to be visible or adjusting their opacity. Combined with differenttransitionThe effect can make this additional information emerge in a more vivid way.

Q3: If I don't want to directly modify the template file, is there a simpler way to add a hover effect to the Banner?

A3: